Skip to content

Instantly share code, notes, and snippets.

@neelabalan

neelabalan/app.py

Created May 31, 2021
Embed
What would you like to do?
Crypto dashboard with dash
import dash
import dash_table
import dash_core_components as dcc
import dash_html_components as html
from dash_html_components.Div import Div
from pycoingecko import CoinGeckoAPI
external_stylesheets = ["https://codepen.io/chriddyp/pen/bWLwgP.css"]
app = dash.Dash(__name__, external_stylesheets=external_stylesheets, update_title=None)
cg = CoinGeckoAPI()
text_style = {
"textAlign": "center",
"fontFamily": "DejaVu Sans Mono",
}
app.layout = html.Div(
[
dcc.Interval(
id="interval-component", interval=10000, n_intervals=0 # in milliseconds
),
html.H1(
children="0",
id="live-price-update",
style=text_style,
),
html.Div(
[
html.Label("Dropdown"),
dcc.Dropdown(
id="currency",
options=[
{"label": "USD", "value": "usd"},
{"label": "INR", "value": "inr"},
{"label": "EUR", "value": "eur"},
],
value="usd",
),
]
),
html.Br(), html.Br(), html.Br(), html.Br(),
html.H4(children="In $", style=text_style),
dash_table.DataTable(
id="table",
columns=[
{"name": i, "id": i}
for i in ["Coin", "Price", "Market Cap", "24h Volume", "24h Change"]
],
data=[],
export_format="csv"
),
]
)
@app.callback(
dash.dependencies.Output("live-price-update", "children"),
[
dash.dependencies.Input("interval-component", "n_intervals"),
dash.dependencies.Input("currency", "value"),
],
)
def update_price(children, value):
currency_symbol_map = {"eur": "€ ", "inr": "₹ ", "usd": "$ "}
return currency_symbol_map.get(value) + str(
cg.get_price(ids="bitcoin", vs_currencies=value)["bitcoin"][value]
)
@app.callback(
dash.dependencies.Output("table", "data"),
[dash.dependencies.Input("interval-component", "n_intervals")],
)
def generate_table(data):
response = cg.get_price(
ids=["bitcoin", "litecoin", "ethereum", "tether", "dogecoin"],
vs_currencies="usd",
include_market_cap="true",
include_24hr_vol="true",
include_24hr_change="true",
)
return [
{
"Coin": r,
"Price": response[r]["usd"],
"Market Cap": response[r]["usd_market_cap"],
"24h Volume": response[r]["usd_24h_vol"],
"24h Change": response[r]["usd_24h_change"],
}
for r in response
]
if __name__ == "__main__":
app.run_server(debug=True)
appdirs==1.4.4
Brotli==1.0.9
certifi==2020.12.5
chardet==4.0.0
click==8.0.1
dash==1.20.0
dash-core-components==1.16.0
dash-html-components==1.1.3
dash-renderer==1.9.1
dash-table==4.11.3
Flask==2.0.1
Flask-Compress==1.9.0
future==0.18.2
idna==2.10
itsdangerous==2.0.1
Jinja2==3.0.1
MarkupSafe==2.0.1
mypy-extensions==0.4.3
pathspec==0.8.1
plotly==4.14.3
pycoingecko==2.0.0
regex==2021.4.4
requests==2.25.1
retrying==1.3.3
six==1.16.0
toml==0.10.2
urllib3==1.26.4
Werkzeug==2.0.1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment