Skip to content

Instantly share code, notes, and snippets.

@pikhovkin
Created September 30, 2018 19:51
Show Gist options
  • Save pikhovkin/6ec23d425b12b720651942fd6a5cdf13 to your computer and use it in GitHub Desktop.
Save pikhovkin/6ec23d425b12b720651942fd6a5cdf13 to your computer and use it in GitHub Desktop.
Dash simple example on Django
from datetime import datetime as dt
from django.conf import settings
from django.conf.urls import include, url
from dash import BaseDashView, 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
class DashSimpleExamppleView(BaseDashView):
dash_name = 'dash_simple_example'
dash = Dash()
dash.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'})
dash.css.append_css({'external_url': 'https://codepen.io/chriddyp/pen/bWLwgP.css'})
@staticmethod
@dash.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}}
}
urlpatterns = [
url(r'^dash/', include('dash.urls')),
]
if __name__ == '__main__':
import os
import sys
from django.core.management import execute_from_command_line
settings.configure(
DEBUG=True,
MIDDLEWARE_CLASSES = [],
ROOT_URLCONF = os.path.splitext(os.path.basename(__file__))[0]
)
execute_from_command_line([sys.argv[0], 'runserver'])
pip install -r requirements.txt
pip install dash_core_components==0.30.2 --no-deps
pip install dash_html_components==0.13.2 --no-deps
Django
plotly==3.3.0
dash_renderer==0.14.1
pandas_datareader==0.7.0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment