pythonbook/fastapi-plotly-dash-master/dashapp.py

62 lines
1.8 KiB
Python

import dash
from dash.dependencies import Input, Output
from dash import dcc, html
import flask
import pandas as pd
import os
def create_dash_app(requests_pathname_prefix: str = None) -> dash.Dash:
"""
Sample Dash application from Plotly: https://github.com/plotly/dash-hello-world/blob/master/app.py
"""
server = flask.Flask(__name__)
server.secret_key = os.environ.get('secret_key', 'secret')
df = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/hello-world-stock.csv')
app = dash.Dash(__name__, server=server, requests_pathname_prefix=requests_pathname_prefix)
app.scripts.config.serve_locally = False
dcc._js_dist[0]['external_url'] = 'https://cdn.plot.ly/plotly-basic-latest.min.js'
app.layout = html.Div([
html.H1('Stock Tickers'),
dcc.Dropdown(
id='my-dropdown',
options=[
{'label': 'Tesla', 'value': 'TSLA'},
{'label': 'Apple', 'value': 'AAPL'},
{'label': 'Coke', 'value': 'COKE'}
],
value='TSLA'
),
dcc.Graph(id='my-graph')
], className="container")
@app.callback(Output('my-graph', 'figure'),
[Input('my-dropdown', 'value')])
def update_graph(selected_dropdown_value):
dff = df[df['Stock'] == selected_dropdown_value]
return {
'data': [{
'x': dff.Date,
'y': dff.Close,
'line': {
'width': 3,
'shape': 'spline'
}
}],
'layout': {
'margin': {
'l': 30,
'r': 20,
'b': 30,
't': 20
}
}
}
return app