abolfazle80 commited on
Commit
516e5ec
Β·
verified Β·
1 Parent(s): 1f43951

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -8
app.py CHANGED
@@ -41,16 +41,17 @@ def get_crypto_price_from_coinex(symbol):
41
  st.error(f"Error fetching data: {e}")
42
  return ["Error", "Error"]
43
 
44
- # Function to download existing CSV from Hugging Face
45
  def download_existing_csv():
46
  try:
47
  response = requests.get(HF_CSV_URL)
48
  response.raise_for_status()
49
 
50
- csv_data = io.StringIO(response.text)
51
- df = pd.read_csv(csv_data)
52
-
53
- return df # Return existing DataFrame
 
54
  except requests.exceptions.RequestException:
55
  return pd.DataFrame() # Return empty DataFrame if file doesn't exist
56
 
@@ -80,8 +81,11 @@ def fetch_data():
80
  timestamp = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
81
  all_data.append(timestamp)
82
 
83
- # Load existing data
84
- df_existing = download_existing_csv()
 
 
 
85
 
86
  # Convert new data to DataFrame
87
  df_new = pd.DataFrame([all_data], columns=[f"{m} Price" for m in markets] +
@@ -123,8 +127,13 @@ if st.button("Stop Fetching Data"):
123
 
124
  # Show Latest Data
125
  st.text("πŸ“Š Latest Data from CSV:")
 
 
 
 
 
126
  try:
127
  df = pd.read_csv(CSV_FILENAME)
128
- st.write(df.tail())
129
  except Exception as e:
130
  st.error(f"Error reading CSV: {e}")
 
41
  st.error(f"Error fetching data: {e}")
42
  return ["Error", "Error"]
43
 
44
+ # Function to download CSV from Hugging Face and save it locally
45
  def download_existing_csv():
46
  try:
47
  response = requests.get(HF_CSV_URL)
48
  response.raise_for_status()
49
 
50
+ with open(CSV_FILENAME, "wb") as file:
51
+ file.write(response.content)
52
+
53
+ df = pd.read_csv(CSV_FILENAME)
54
+ return df
55
  except requests.exceptions.RequestException:
56
  return pd.DataFrame() # Return empty DataFrame if file doesn't exist
57
 
 
81
  timestamp = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
82
  all_data.append(timestamp)
83
 
84
+ # Check if CSV exists locally; if not, download it
85
+ if not os.path.exists(CSV_FILENAME):
86
+ df_existing = download_existing_csv()
87
+ else:
88
+ df_existing = pd.read_csv(CSV_FILENAME)
89
 
90
  # Convert new data to DataFrame
91
  df_new = pd.DataFrame([all_data], columns=[f"{m} Price" for m in markets] +
 
127
 
128
  # Show Latest Data
129
  st.text("πŸ“Š Latest Data from CSV:")
130
+
131
+ # Ensure the file exists before reading it
132
+ if not os.path.exists(CSV_FILENAME):
133
+ download_existing_csv()
134
+
135
  try:
136
  df = pd.read_csv(CSV_FILENAME)
137
+ st.write(df.tail()) # Show last 5 rows
138
  except Exception as e:
139
  st.error(f"Error reading CSV: {e}")