|
| 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) |
0 commit comments