abolfazle80 commited on
Commit
bf8b3ab
·
verified ·
1 Parent(s): cd98fa3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -11
app.py CHANGED
@@ -54,33 +54,59 @@ if not file_exists:
54
  header.append(f'{market} Volume')
55
  header.append('Timestamp') # Add timestamp to the end
56
  writer.writerow(header)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
57
 
58
- # Function to fetch data in a loop
59
  def fetch_data():
 
60
  global loop_running
61
  while loop_running:
62
  try:
63
  all_data = []
64
  for market in markets:
65
  crypto_data = get_crypto_price_from_coinex(market)
66
- all_data += crypto_data # Add price and volume to the list
67
 
68
  timestamp = datetime.now().strftime('%Y-%m-%d %H:%M:%S') # Get the current timestamp
69
- all_data.append(timestamp) # Add the timestamp to the data list
70
 
71
- # Append the data to the CSV file
72
- with open(filename, 'a', newline='') as file:
 
73
  writer = csv.writer(file)
74
- writer.writerow(all_data)
 
 
 
 
 
 
75
 
76
- # Print confirmation to console
77
- print(f'{all_data} data added')
78
 
79
- # Sleep for 1 second before the next iteration
80
- time.sleep(1)
81
  except Exception as e:
82
  st.error(f"An error occurred: {e}")
83
- loop_running = False # Stop the loop in case of an error
84
 
85
  # Streamlit UI elements
86
  st.title("Live Cryptocurrency Data with Start/Stop Loop")
 
54
  header.append(f'{market} Volume')
55
  header.append('Timestamp') # Add timestamp to the end
56
  writer.writerow(header)
57
+ from huggingface_hub import HfApi
58
+ import os
59
+
60
+ HF_USERNAME = "abolfazle80" # Your Hugging Face username
61
+ REPO_NAME = "crypto_data" # Your dataset repo
62
+
63
+ api = HfApi()
64
+
65
+ def upload_to_huggingface():
66
+ """Uploads the CSV file to Hugging Face, preserving all appended data."""
67
+ try:
68
+ api.upload_file(
69
+ path_or_fileobj="USDT.csv", # Upload the local CSV
70
+ path_in_repo="USDT.csv", # Save as USDT.csv in the repo
71
+ repo_id=f"{HF_USERNAME}/{REPO_NAME}",
72
+ repo_type="dataset",
73
+ )
74
+ print("CSV successfully uploaded to Hugging Face.")
75
+ except Exception as e:
76
+ print(f"Upload failed: {e}")
77
 
 
78
  def fetch_data():
79
+ """Fetches crypto data, appends to CSV, and uploads to Hugging Face."""
80
  global loop_running
81
  while loop_running:
82
  try:
83
  all_data = []
84
  for market in markets:
85
  crypto_data = get_crypto_price_from_coinex(market)
86
+ all_data += crypto_data # Add price and volume
87
 
88
  timestamp = datetime.now().strftime('%Y-%m-%d %H:%M:%S') # Get the current timestamp
89
+ all_data.append(timestamp) # Append timestamp to the row
90
 
91
+ # Append the data to the CSV file (do not overwrite)
92
+ file_exists = os.path.isfile("USDT.csv") # Check if file exists
93
+ with open("USDT.csv", "a", newline='') as file:
94
  writer = csv.writer(file)
95
+ if not file_exists:
96
+ # Write header if the file is newly created
97
+ header = [f"{m} Price" for m in markets] + [f"{m} Volume" for m in markets] + ["Timestamp"]
98
+ writer.writerow(header)
99
+ writer.writerow(all_data) # Append new row
100
+
101
+ print(f"{all_data} added to CSV")
102
 
103
+ # Upload the updated CSV to Hugging Face
104
+ upload_to_huggingface()
105
 
106
+ time.sleep(1) # Fetch new data every second
 
107
  except Exception as e:
108
  st.error(f"An error occurred: {e}")
109
+ loop_running = False # Stop loop if error occurs
110
 
111
  # Streamlit UI elements
112
  st.title("Live Cryptocurrency Data with Start/Stop Loop")