|
import streamlit as st |
|
import requests |
|
import plotly.graph_objects as go |
|
from datetime import datetime |
|
import time |
|
|
|
|
|
st.title("График криптовалют в реальном времени") |
|
|
|
|
|
crypto_options = ["bitcoin", "ethereum", "litecoin", "dogecoin"] |
|
selected_crypto = st.selectbox("Выберите криптовалюту", crypto_options) |
|
|
|
|
|
def get_crypto_price(crypto): |
|
url = f"https://api.coingecko.com/api/v3/simple/price?ids={crypto}&vs_currencies=usd" |
|
response = requests.get(url) |
|
data = response.json() |
|
return data[crypto]['usd'] |
|
|
|
|
|
prices = [] |
|
times = [] |
|
|
|
|
|
fig = go.Figure() |
|
|
|
|
|
placeholder = st.empty() |
|
|
|
while True: |
|
|
|
price = get_crypto_price(selected_crypto) |
|
current_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S") |
|
|
|
|
|
prices.append(price) |
|
times.append(current_time) |
|
|
|
|
|
fig.add_trace(go.Scatter(x=times, y=prices, mode='lines', name=selected_crypto)) |
|
fig.update_layout(title=f"Цена {selected_crypto.capitalize()} в USD", |
|
xaxis_title="Время", |
|
yaxis_title="Цена в USD") |
|
|
|
|
|
placeholder.plotly_chart(fig, use_container_width=True) |
|
|
|
|
|
fig.data = [] |
|
|
|
|
|
time.sleep(10) |