Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,54 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import requests
|
3 |
+
import pandas as pd
|
4 |
+
from datetime import datetime
|
5 |
+
|
6 |
+
# Define the markets (cryptocurrencies to monitor)
|
7 |
+
markets = ['DOTUSDT', 'BTCUSDT', 'ADAUSDT', 'BNBUSDT', 'SUIUSDT', 'XRPUSDT']
|
8 |
+
|
9 |
+
# Function to get price and volume data from CoinEx
|
10 |
+
def get_crypto_price_from_coinex(symbol):
|
11 |
+
url = 'https://api.coinex.com/v1/market/ticker'
|
12 |
+
params = {'market': symbol}
|
13 |
+
|
14 |
+
try:
|
15 |
+
response = requests.get(url, params=params)
|
16 |
+
response.raise_for_status() # Check for HTTP errors
|
17 |
+
data = response.json()
|
18 |
+
|
19 |
+
if 'data' in data:
|
20 |
+
price = data['data']['ticker']['last']
|
21 |
+
volume = data['data']['ticker']['vol']
|
22 |
+
return [price, volume] # Return the price and volume as a list
|
23 |
+
else:
|
24 |
+
return ["Symbol not found", "Symbol not found"]
|
25 |
+
except requests.exceptions.RequestException as e:
|
26 |
+
st.error(f"Request error: {e}")
|
27 |
+
return ["Request error", "Request error"]
|
28 |
+
except ValueError as e:
|
29 |
+
st.error(f"JSON decode error: {e}")
|
30 |
+
return ["JSON decode error", "JSON decode error"]
|
31 |
+
|
32 |
+
# Streamlit UI elements
|
33 |
+
st.title("Live Cryptocurrency Data")
|
34 |
+
|
35 |
+
# Create a button to fetch the data
|
36 |
+
if st.button("Fetch Live Data"):
|
37 |
+
# Create a list to store data for all markets
|
38 |
+
all_data = []
|
39 |
+
|
40 |
+
# Fetch data for each market
|
41 |
+
for market in markets:
|
42 |
+
crypto_data = get_crypto_price_from_coinex(market)
|
43 |
+
all_data.append([market, crypto_data[0], crypto_data[1]]) # [market, price, volume]
|
44 |
+
|
45 |
+
# Convert the data into a DataFrame for easier display
|
46 |
+
df = pd.DataFrame(all_data, columns=["Market", "Price (USDT)", "Volume (24h)"])
|
47 |
+
df['Timestamp'] = datetime.now().strftime('%Y-%m-%d %H:%M:%S') # Add a timestamp column
|
48 |
+
|
49 |
+
# Display the table
|
50 |
+
st.write(df)
|
51 |
+
|
52 |
+
# To update the page every 30 seconds to show live data:
|
53 |
+
st.text("Refreshing data every 30 seconds...")
|
54 |
+
st.experimental_rerun() # Automatically rerun the app to update the data every 30 seconds
|