Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,75 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import requests
|
2 |
+
import pandas as pd
|
3 |
+
import streamlit as st
|
4 |
+
import time
|
5 |
+
import plotly.express as px
|
6 |
+
|
7 |
+
# Configura tu clave de RapidAPI
|
8 |
+
api_key = "c4b66a3c25msh950412fab4eaee1p159dbfjsn4"
|
9 |
+
|
10 |
+
# Funci贸n para obtener datos de las criptomonedas
|
11 |
+
def obtener_datos_criptomonedas():
|
12 |
+
url = "https://coingecko.p.rapidapi.com/coins/markets"
|
13 |
+
querystring = {"vs_currency": "usd", "order": "market_cap_desc", "per_page": "25", "page": "1"}
|
14 |
+
headers = {
|
15 |
+
"x-rapidapi-host": "coingecko.p.rapidapi.com",
|
16 |
+
"x-rapidapi-key": api_key
|
17 |
+
}
|
18 |
+
|
19 |
+
response = requests.get(url, headers=headers, params=querystring)
|
20 |
+
|
21 |
+
if response.status_code == 200:
|
22 |
+
return response.json()
|
23 |
+
else:
|
24 |
+
st.error("Error al obtener los datos de la API")
|
25 |
+
return []
|
26 |
+
|
27 |
+
# Funci贸n para convertir los datos en un DataFrame
|
28 |
+
def crear_dataframe(datos):
|
29 |
+
data = []
|
30 |
+
for moneda in datos:
|
31 |
+
data.append({
|
32 |
+
"ID": moneda["id"],
|
33 |
+
"Simbolo": moneda["symbol"].upper(),
|
34 |
+
"Nombre": moneda["name"],
|
35 |
+
"Precio (USD)": moneda["current_price"],
|
36 |
+
"Volumen (24h)": moneda["total_volume"],
|
37 |
+
"Capitalizaci贸n de Mercado": moneda["market_cap"],
|
38 |
+
"Cambio 24h (%)": moneda["price_change_percentage_24h"]
|
39 |
+
})
|
40 |
+
|
41 |
+
return pd.DataFrame(data)
|
42 |
+
|
43 |
+
# Configuraci贸n de Streamlit
|
44 |
+
st.title("Top 25 Criptomonedas M谩s Valiosas")
|
45 |
+
st.write("Informaci贸n actualizada de las criptomonedas m谩s valiosas por capitalizaci贸n de mercado.")
|
46 |
+
|
47 |
+
# Bot贸n para actualizar los datos
|
48 |
+
if st.button("Actualizar Datos"):
|
49 |
+
datos = obtener_datos_criptomonedas()
|
50 |
+
st.session_state['datos'] = datos
|
51 |
+
|
52 |
+
# Verificar si ya existen datos en la sesi贸n
|
53 |
+
if 'datos' not in st.session_state:
|
54 |
+
st.session_state['datos'] = obtener_datos_criptomonedas()
|
55 |
+
|
56 |
+
# Crear el DataFrame
|
57 |
+
df = crear_dataframe(st.session_state['datos'])
|
58 |
+
|
59 |
+
# Mostrar los datos en una tabla
|
60 |
+
st.write("### Tabla de Criptomonedas")
|
61 |
+
st.dataframe(df)
|
62 |
+
|
63 |
+
# Mostrar gr谩fico de precios
|
64 |
+
st.write("### Gr谩fico de Precios (USD)")
|
65 |
+
fig = px.line(df, x="Nombre", y="Precio (USD)", title="Precios de las Criptomonedas")
|
66 |
+
st.plotly_chart(fig)
|
67 |
+
|
68 |
+
# Mostrar gr谩fico de fluctuaciones de precio en 24 horas
|
69 |
+
st.write("### Fluctuaci贸n en las 煤ltimas 24 horas (%)")
|
70 |
+
fig_change = px.bar(df, x="Nombre", y="Cambio 24h (%)", title="Cambio de Precio en 24h (%)")
|
71 |
+
st.plotly_chart(fig_change)
|
72 |
+
|
73 |
+
# Actualizar cada 10 minutos (opcional)
|
74 |
+
st.write("Los datos se actualizan cada 10 minutos autom谩ticamente.")
|
75 |
+
st.autorefresh(interval=600000) # 10 minutos en milisegundos
|