Skip to content
This repository was archived by the owner on Jun 4, 2024. It is now read-only.

Commit d001145

Browse files
committed
more examples
1 parent eb3da64 commit d001145

10 files changed

+240
-0
lines changed

dash-animage-range.gif

1.23 MB
Loading

dash-animate-range.py

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import dash
2+
import dash_core_components as dcc
3+
import dash_html_components as html
4+
5+
import numpy as np
6+
7+
app = dash.Dash()
8+
9+
value_range = [-5, 5]
10+
11+
app.layout = html.Div([
12+
html.Div([
13+
html.Div(dcc.Graph(animate=True, id='graph-1'), className="six columns"),
14+
html.Div(dcc.Graph(animate=True, id='graph-2'), className="six columns")
15+
], className="row"),
16+
dcc.RangeSlider(
17+
id='slider',
18+
min=value_range[0],
19+
max=value_range[1],
20+
step=1,
21+
value=[-2, 2],
22+
marks={i: i for i in range(value_range[0], value_range[1]+1)}
23+
)
24+
], className="container")
25+
26+
trace = {
27+
'mode': 'markers',
28+
'marker': {
29+
'size': 12,
30+
'opacity': 0.5,
31+
'line': {
32+
'width': 0.5,
33+
'color': 'white'
34+
}
35+
}
36+
}
37+
38+
@app.callback(
39+
dash.dependencies.Output('graph-1', 'figure'),
40+
[dash.dependencies.Input('slider', 'value')])
41+
def update_graph_1(value):
42+
x = np.random.rand(50) * (value[1] - value[0]) + value[0]
43+
y = np.random.rand(50) * (value[1] - value[0]) + value[0]
44+
return {
45+
'data': [dict({'x': x, 'y': y}, **trace)],
46+
'layout': {
47+
'xaxis': {'range': [np.min(x), np.max(x)]},
48+
'yaxis': {'range': [np.min(y), np.max(y)]}
49+
}
50+
}
51+
52+
@app.callback(
53+
dash.dependencies.Output('graph-2', 'figure'),
54+
[dash.dependencies.Input('slider', 'value')])
55+
def update_graph_1(value):
56+
x = np.random.rand(50) * (value[1] - value[0]) + value[0]
57+
y = np.random.rand(50) * (value[1] - value[0]) + value[0]
58+
return {
59+
'data': [dict({'x': x, 'y': y}, **trace)],
60+
'layout': {
61+
'xaxis': {'range': value_range},
62+
'yaxis': {'range': value_range}
63+
}
64+
}
65+
66+
app.css.append_css({"external_url": "https://codepen.io/chriddyp/pen/bWLwgP.css"})
67+
68+
if __name__ == '__main__':
69+
app.run_server(debug=True)

dash-asynchronous.gif

25.9 KB
Loading

dash-asynchronous.py

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import dash
2+
from dash.dependencies import Input, Output, Event
3+
import dash_core_components as dcc
4+
import dash_html_components as html
5+
6+
import datetime
7+
import time
8+
9+
class Semaphore:
10+
def __init__(self, filename='semaphore.txt'):
11+
self.filename = filename
12+
with open(self.filename, 'w') as f:
13+
f.write('done')
14+
15+
def lock(self):
16+
with open(self.filename, 'w') as f:
17+
f.write('working')
18+
19+
def unlock(self):
20+
with open(self.filename, 'w') as f:
21+
f.write('done')
22+
23+
def is_locked(self):
24+
return open(self.filename, 'r').read() == 'working'
25+
26+
semaphore = Semaphore()
27+
28+
def long_process():
29+
if semaphore.is_locked():
30+
raise Exception('Resource is locked')
31+
semaphore.lock()
32+
time.sleep(7)
33+
semaphore.unlock()
34+
return datetime.datetime.now()
35+
36+
app = dash.Dash()
37+
38+
def layout():
39+
return html.Div([
40+
html.Button('Run Process', id='button'),
41+
dcc.Interval(id='interval', interval=500),
42+
dcc.RadioItems(
43+
id='lock',
44+
options=[{'label': i, 'value': i} for i in ['Running...', 'Free']]),
45+
html.Div(id='output')
46+
])
47+
48+
app.layout = layout
49+
50+
@app.callback(
51+
Output('lock', 'value'),
52+
events=[Event('interval', 'interval')])
53+
def display_status():
54+
return 'Running...' if semaphore.is_locked() else 'Free'
55+
56+
@app.callback(
57+
Output('output', 'children'),
58+
events=[Event('button', 'click')])
59+
def run_process():
60+
return 'Finished at {}'.format(long_process())
61+
62+
app.scripts.config.serve_locally = True
63+
64+
if __name__ == '__main__':
65+
app.run_server(debug=True, processes=5)

dash_requests.py

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import dash
2+
from dash.dependencies import Input, Output
3+
import dash_core_components as dcc
4+
import dash_html_components as html
5+
6+
import flask
7+
8+
app = dash.Dash()
9+
app.layout = html.Div([
10+
dcc.Dropdown(
11+
id='d',
12+
options=[{'label': i, 'value': i} for i in ['a', 'b', 'c']],
13+
value='a'
14+
),
15+
html.Div(id='output')
16+
])
17+
18+
19+
@app.callback(
20+
Output('output', 'children'),
21+
[Input('d', 'value')])
22+
def update_outout(value):
23+
print(flask.request.cookies)
24+
return value
25+
26+
if __name__ == '__main__':
27+
app.run_server(debug=True)

live-reload.gif

333 KB
Loading

live-reload.py

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import dash
2+
from dash.dependencies import Event, Input, Output
3+
import dash_core_components as dcc
4+
import dash_html_components as html
5+
6+
app = dash.Dash()
7+
8+
# Edit this object!
9+
layout = html.Div([
10+
html.H1('Hello Dash!'),
11+
html.H3('Dash with live-reload'),
12+
])
13+
14+
# Barebones layout
15+
app.layout = html.Div([
16+
dcc.Interval(id='refresh', interval=200),
17+
html.Div(id='content', className="container")
18+
])
19+
20+
# Update the `content` div with the `layout` object.
21+
# When you save this file, `debug=True` will re-run
22+
# this script, serving the new layout
23+
@app.callback(
24+
Output('content', 'children'),
25+
events=[Event('refresh', 'interval')])
26+
def display_layout():
27+
return layout
28+
29+
app.css.append_css({"external_url": "https://codepen.io/chriddyp/pen/bWLwgP.css"})
30+
31+
if __name__ == '__main__':
32+
app.run_server(debug=True)

semaphore.txt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
done

urls/dash-navigation.gif

227 KB
Loading

urls/multi_page.py

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# pip install dash_core_components==0.5.3rc1
2+
3+
import dash
4+
from dash.dependencies import Input, Output
5+
import dash_html_components as html
6+
import dash_core_components as dcc
7+
8+
app = dash.Dash()
9+
10+
app.layout = html.Div([
11+
# This "header" will persist across pages
12+
html.H2('Multi Page Dash App'),
13+
14+
# Each "page" will modify this element
15+
html.Div(id='content-container'),
16+
17+
# This Location component represents the URL bar
18+
dcc.Location(id='url', refresh=False)
19+
], className="container")
20+
21+
@app.callback(
22+
Output('content-container', 'children'),
23+
[Input('url', 'pathname')])
24+
def display_page(pathname):
25+
if pathname == '/':
26+
return html.Div([
27+
html.Div('You are on the index page.'),
28+
29+
# the dcc.Link component updates the `Location` pathname
30+
# without refreshing the page
31+
dcc.Link(html.A('Go to page 2 without refreshing!'), href="/page-2"),
32+
html.Hr(),
33+
html.A('Go to page 2 but refresh the page', href="/page-2")
34+
])
35+
elif pathname == '/page-2':
36+
return html.Div([
37+
html.H4('Welcome to Page 2'),
38+
dcc.Link(html.A('Go back home'), href="/"),
39+
])
40+
else:
41+
return html.Div('I guess this is like a 404 - no content available')
42+
43+
app.css.append_css({"external_url": "https://codepen.io/chriddyp/pen/bWLwgP.css"})
44+
45+
if __name__ == '__main__':
46+
app.run_server(debug=True)

0 commit comments

Comments
 (0)