Skip to content

Instantly share code, notes, and snippets.

@xiaopeng163
Created September 3, 2021 09:57
Show Gist options
  • Save xiaopeng163/f39461c96bb23921e12b251564f69c80 to your computer and use it in GitHub Desktop.
Save xiaopeng163/f39461c96bb23921e12b251564f69c80 to your computer and use it in GitHub Desktop.
import dash
from dash.dependencies import Input, Output
import dash_core_components as dcc
import dash_html_components as html
from pandas_datareader import data as web
from datetime import datetime as dt
app = dash.Dash('Hello World')
app.layout = html.Div([
dcc.Dropdown(
id='my-dropdown',
options=[
{'label': 'Coke', 'value': 'COKE'},
{'label': 'Tesla', 'value': 'TSLA'},
{'label': 'Apple', 'value': 'AAPL'}
],
value='COKE'
),
dcc.Graph(id='my-graph')
], style={'width': '500'})
@app.callback(Output('my-graph', 'figure'), [Input('my-dropdown', 'value')])
def update_graph(selected_dropdown_value):
df = web.DataReader(
selected_dropdown_value,
'yahoo',
dt(2017, 1, 1),
dt.now()
)
return {
'data': [{
'x': df.index,
'y': df.Close
}],
'layout': {'margin': {'l': 40, 'r': 0, 't': 20, 'b': 30}}
}
app.css.append_css({'external_url': 'https://codepen.io/chriddyp/pen/bWLwgP.css'})
if __name__ == '__main__':
app.run_server()
@xiaopeng163
Copy link
Author

安装依赖

pip install wheel dash pandas_datareader

启动

$ python helloworld.py
Dash is running on http://127.0.0.1:8050/

 * Serving Flask app 'Hello World' (lazy loading)
 * Environment: production
   WARNING: This is a development server. Do not use it in a production deployment.
   Use a production WSGI server instead.
 * Debug mode: off
 * Running on http://127.0.0.1:8050/ (Press CTRL+C to quit)


Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment