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

Commit 90df598

Browse files
committed
more examples
1 parent 97758bb commit 90df598

35 files changed

+431
-1
lines changed
+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
J�[�Y.�U!Selected "Option 1" at "17:26:47"q.
+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
J�Z�Y.�U2017-08-18 17:23:11.220842q.
+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
J�Z�Y.�U2017-08-18 17:23:31.435322q.
+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
J�[�Y.�U2017-08-18 17:26:37.888685q.
+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
J[�Y.�U2017-08-18 17:24:23.253460q.
22 Bytes
Binary file not shown.
22 Bytes
Binary file not shown.
22 Bytes
Binary file not shown.
+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
J�Z�Y.�U2017-08-18 17:22:42.955196q.

cache/tmp8bwPP2.__wz_cache

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
JZ�Y.�

cache/tmp9d3gdx.__wz_cache

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
J Z�Y.�

cache/tmpIRYm0g.__wz_cache

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
J�Y�Y.�

cache/tmpMHMVTn.__wz_cache

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
J Z�Y.�

cache/tmpaCeitG.__wz_cache

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
JZ�Y.�

cache/tmpb2qIOD.__wz_cache

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
J�Y�Y.�

cache/tmpbHotyS.__wz_cache

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
J�Y�Y.�

cache/tmph1ad6Q.__wz_cache

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
JZ�Y.�

cache/tmpjC0IVC.__wz_cache

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
JZ�Y.�

cache/tmppYUtvS.__wz_cache

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
J�Y�Y.�

cache/tmprOLIx2.__wz_cache

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
J�Y�Y.�

cache/tmpwGUwON.__wz_cache

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
J Z�Y.�

dash-cache-layout.py

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import dash
2+
from dash.dependencies import Input, Output
3+
import dash_html_components as html
4+
import dash_core_components as dcc
5+
import datetime
6+
import os
7+
from flask_caching import Cache
8+
9+
10+
app = dash.Dash(__name__)
11+
cache = Cache(app.server, config={
12+
'CACHE_TYPE': 'filesystem',
13+
'CACHE_DIR': 'cache'
14+
})
15+
app.config.supress_callback_exceptions = True
16+
17+
timeout = 20
18+
19+
@cache.memoize(timeout=timeout)
20+
def compute_expensive_data():
21+
return str(datetime.datetime.now())
22+
23+
24+
def generate_layout():
25+
expensive_data = compute_expensive_data()
26+
return html.Div([
27+
html.H3('Last updated at: ' + expensive_data),
28+
html.Div(id='flask-cache-memoized-children'),
29+
dcc.RadioItems(
30+
id='flask-cache-memoized-dropdown',
31+
options=[
32+
{'label': 'Option {}'.format(i), 'value': 'Option {}'.format(i)}
33+
for i in range(1, 4)
34+
],
35+
value='Option 1'
36+
),
37+
html.Div('Results are cached for {} seconds'.format(timeout))
38+
])
39+
40+
app.layout = generate_layout
41+
42+
43+
@app.callback(
44+
Output('flask-cache-memoized-children', 'children'),
45+
[Input('flask-cache-memoized-dropdown', 'value')])
46+
@cache.memoize(timeout=timeout)
47+
def render(value):
48+
return 'Selected "{}" at "{}"'.format(
49+
value, datetime.datetime.now().strftime('%H:%M:%S')
50+
)
51+
52+
53+
if __name__ == '__main__':
54+
app.run_server(debug=True)

dash-callback-factory.py

+141
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
from datetime import datetime
2+
3+
from dash import dash
4+
5+
import dash_core_components as dcc
6+
import dash_html_components as html
7+
from dash.dependencies import Input, Output, Event, State
8+
from dash.exceptions import CantHaveMultipleOutputs
9+
10+
app = dash.Dash(__name__)
11+
app.config.supress_callback_exceptions = True
12+
13+
IDS = [1, 2, 3]
14+
15+
16+
def divs_list():
17+
return [html.Div([
18+
dcc.Markdown(
19+
'',
20+
id='model-{}-markdown'.format(id)
21+
),
22+
html.P(
23+
'',
24+
id='model-{}-p'.format(id)
25+
),
26+
html.Button(
27+
'Delete',
28+
id='model-{}-delete-button'.format(id),
29+
style={'width': '49%'}
30+
),
31+
html.Button(
32+
'Start/Stop',
33+
id='model-{}-toggle-button'.format(id),
34+
style={'marginLeft': '2%', 'width': '49%'}
35+
),
36+
37+
html.Hr()
38+
], id='model-k2-{}'.format(id)) for id in IDS]
39+
40+
41+
def create_callbacks():
42+
def delete(i):
43+
def callback(*data):
44+
return """# Delete
45+
{}""".format(datetime.now().time().isoformat())
46+
47+
return callback
48+
49+
def toggle(i):
50+
def callback(*data):
51+
return "Toggle {}".format(datetime.now().time().isoformat())
52+
53+
return callback
54+
55+
for model_id in IDS:
56+
try:
57+
app.callback(Output('model-{}-markdown'.format(model_id), 'children'),
58+
events=[Event('model-{}-delete-button'.format(model_id), 'click')])(delete(model_id))
59+
60+
app.callback(Output('model-{}-p'.format(model_id), 'children'),
61+
events=[Event('model-{}-toggle-button'.format(model_id), 'click')])(toggle(model_id))
62+
except CantHaveMultipleOutputs:
63+
continue
64+
65+
66+
def layout():
67+
divs = divs_list()
68+
69+
return [html.Div([
70+
html.H1('Example'),
71+
72+
html.Div(divs, id='model-k2-divs'),
73+
], style={'maxWidth': '720px', 'margin': '0 auto'}, id='example')]
74+
75+
app.layout = html.Div(children=[
76+
html.Meta(
77+
name='viewport',
78+
content='width=device-width, initial-scale=1.0'
79+
),
80+
81+
html.Div([
82+
html.Div([
83+
html.Div(
84+
html.Div(id="page", className="content"),
85+
className="content-container"
86+
),
87+
], className="container-width")
88+
], className="background"),
89+
90+
dcc.Location(id='location', refresh=False),
91+
], style={'width': '70%', 'margin': '0 auto'})
92+
93+
94+
toc = html.Div([
95+
html.Ul([
96+
html.Li(dcc.Link('Home', href='/')),
97+
html.Li(dcc.Link('Example', href='/example')),
98+
])
99+
])
100+
101+
pages = {
102+
'index': {
103+
'url': '/',
104+
'content': html.Div([
105+
html.H1('Table of Content'),
106+
toc,
107+
], className="toc", style={'textAlign': 'center'})
108+
},
109+
'example': {
110+
'url': '/example',
111+
'content': layout
112+
}
113+
}
114+
115+
116+
@app.callback(
117+
Output('page', 'children'),
118+
[Input('location', 'pathname')])
119+
def display_content(pathname):
120+
if pathname is None:
121+
return html.Div()
122+
matched = [c for c in pages.keys()
123+
if pages[c]['url'] == pathname]
124+
125+
if matched and matched[0] == 'example':
126+
create_callbacks()
127+
c = pages[matched[0]]['content']
128+
page_content = c()
129+
content = html.Div(page_content)
130+
else:
131+
content = pages['index']['content']
132+
133+
return content
134+
135+
app.css.append_css({"external_url": "https://codepen.io/chriddyp/pen/bWLwgP.css"})
136+
137+
if __name__ == '__main__':
138+
app.run_server(
139+
debug=True,
140+
threaded=True,
141+
port=5005)

dash-display-error-messages.py

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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+
import flask
6+
import os
7+
8+
app = dash.Dash()
9+
10+
app.layout = html.Div([
11+
dcc.Input(id='input', value=''),
12+
html.Div(id='output')
13+
])
14+
15+
@app.callback(
16+
Output('output', 'children'),
17+
[Input('input', 'value')])
18+
def output(value):
19+
flask.session['error-message'] = 'test'
20+
if value == 'error':
21+
1/0
22+
else:
23+
return value
24+
25+
app.scripts.append_script({
26+
'external_url': '/static/dash-error-message-display.js'
27+
})
28+
29+
@app.server.route('/static/<filename>.js')
30+
def serve_script(filename):
31+
print('serving {}'.format(filename))
32+
if filename not in ['dash-error-message-display']:
33+
raise Exception('"{}" is excluded from the allowed static files'.format(filename))
34+
return flask.send_from_directory(os.getcwd(), '{}.js'.format(filename))
35+
36+
37+
if __name__ == '__main__':
38+
app.run_server(debug=True)

dash-error-message-display.js

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
var errorId = 'error-message-container'
2+
var body = document.getElementsByTagName('body')[0]
3+
var newNode = document.createElement('div');
4+
newNode.setAttribute('id', errorId);
5+
body.appendChild(newNode);
6+
7+
console.warn('adding window.onerror');
8+
window.onerror = function(msg, url, line, col, error) {
9+
var errorElement = document.getElementById(errorId);
10+
console.warn('window.onerror', msg);
11+
errorElement.innerHtml = ([
12+
'<div>',
13+
'<hr/>',
14+
'<div style="font-size: 14px;">Error occurred!</div>',
15+
'<pre style="color: #FF4136">',
16+
error,
17+
'</pre>',
18+
'</div>'
19+
].join(''));
20+
};

dash-network-graph.gif

266 KB
Loading

dash-network-graph.py

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import dash
2+
import dash_core_components as dcc
3+
import dash_html_components as html
4+
from dash.dependencies import Input, Output, Event, State
5+
6+
import visdcc
7+
8+
app = dash.Dash()
9+
10+
app.layout = html.Div([
11+
visdcc.Network(id='net',
12+
data={'nodes':[{'id': 1, 'label': 'Node 1', 'color':'#00ffff'},
13+
{'id': 2, 'label': 'Node 2'},
14+
{'id': 4, 'label': 'Node 4'},
15+
{'id': 5, 'label': 'Node 5'},
16+
{'id': 6, 'label': 'Node 6'}],
17+
'edges':[{'id':'1-3', 'from': 1, 'to': 3},
18+
{'id':'1-2', 'from': 1, 'to': 2}]
19+
},
20+
options=dict(height='600px', width='100%')),
21+
dcc.RadioItems(id='color',
22+
options=[{'label': 'Red' , 'value': '#ff0000'},
23+
{'label': 'Green', 'value': '#00ff00'},
24+
{'label': 'Blue' , 'value': '#0000ff'}],
25+
value='Red')
26+
])
27+
28+
@app.callback(
29+
Output('net', 'options'),
30+
[Input('color', 'value')])
31+
def myfun(x):
32+
return {'nodes':{'color': x}}
33+
34+
if __name__ == '__main__':
35+
app.run_server(debug=True)

dash-urls-hide-show-tabs.py

-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import dash_core_components as dcc
44
import dash_html_components as html
55

6-
76
app = dash.Dash()
87

98

large-data.py

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import dash
2+
import dash_core_components as dcc
3+
import dash_html_components as html
4+
5+
import json
6+
import numpy as np
7+
8+
app = dash.Dash()
9+
10+
N = 100*1000*1000
11+
12+
app.layout = html.Div([
13+
html.Div(
14+
dcc.Graph(
15+
id='graph',
16+
figure={
17+
'data': [{
18+
'x': np.random.randn(N, 1),
19+
'Y': np.random.randn(N, 1)
20+
}]
21+
}
22+
), style={
23+
'display': 'none'
24+
}
25+
),
26+
dcc.Input(id='input', value=''),
27+
html.Div(id='output', style={'display': 'none'})
28+
])
29+
30+
31+
@app.callback(
32+
dash.dependencies.Output('output', 'children'),
33+
[dash.dependencies.Input('input', 'value')])
34+
def update_output(value):
35+
return json.dumps(range(N))
36+
37+
if __name__ == '__main__':
38+
app.run_server(debug=True)

0 commit comments

Comments
 (0)