Advertisement
Guest User

Untitled

a guest
May 20th, 2019
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.44 KB | None | 0 0
  1. import dash
  2. import dash_bootstrap_components as dbc
  3. import dash_core_components as dcc
  4. import dash_html_components as html
  5. from dash.dependencies import Input, Output, State
  6.  
  7.  
  8. def parse_query_params(s):
  9. s = s.lstrip("?")
  10. return dict(pair.split("=") for pair in s.split("&"))
  11.  
  12.  
  13. def create_deepdive_layout(cluster=None):
  14. try:
  15. cluster = int(cluster)
  16. except TypeError:
  17. cluster = None
  18.  
  19. cluster_dropdown = dcc.Dropdown(
  20. id="cluster-dropdown",
  21. options=[
  22. {"label": "Cluster 1", "value": 1},
  23. {"label": "Cluster 2", "value": 2},
  24. ],
  25. value=cluster,
  26. )
  27. return [
  28. html.H2("Deepdive"),
  29. html.Hr(),
  30. dbc.Row([dbc.Col(cluster_dropdown), dbc.Col(id="deepdive-output")]),
  31. ]
  32.  
  33.  
  34. overview_layout = [
  35. html.H2("Overview"),
  36. html.Hr(),
  37. dbc.Row(
  38. [
  39. dbc.Col(
  40. dbc.Card(
  41. [
  42. html.P("This link corresponds to cluster 1"),
  43. dbc.CardLink("Cluster 1", href="/deepdive?cluster=1"),
  44. ],
  45. body=True,
  46. )
  47. ),
  48. dbc.Col(
  49. dbc.Card(
  50. [
  51. html.P("This link corresponds to cluster 2"),
  52. dbc.CardLink("Cluster 2", href="/deepdive?cluster=2"),
  53. ],
  54. body=True,
  55. )
  56. ),
  57. ]
  58. ),
  59. ]
  60.  
  61. app = dash.Dash(external_stylesheets=[dbc.themes.BOOTSTRAP])
  62. app.config.suppress_callback_exceptions = True
  63.  
  64. app.layout = html.Div(
  65. [
  66. dcc.Location(id="url"),
  67. dbc.NavbarSimple(
  68. children=[
  69. dbc.NavLink("Page 1", href="/overview", id="overview-link"),
  70. dbc.NavLink("Page 2", href="/deepdive", id="deepdive-link"),
  71. ],
  72. brand="Demo",
  73. color="primary",
  74. dark=True,
  75. ),
  76. dbc.Container(id="page-content", className="pt-4"),
  77. ]
  78. )
  79.  
  80.  
  81. # this callback uses the current pathname to set the active state of the
  82. # corresponding nav link to true, allowing users to tell see page they are on
  83. @app.callback(
  84. [Output("overview-link", "active"), Output("deepdive-link", "active")],
  85. [Input("url", "pathname")],
  86. )
  87. def toggle_active_links(pathname):
  88. if pathname == "/":
  89. # Treat overview as the homepage / index
  90. return True, False
  91. return pathname == "/overview", pathname == "/deepdive"
  92.  
  93.  
  94. @app.callback(
  95. Output("page-content", "children"),
  96. [Input("url", "pathname")],
  97. [State("url", "search")],
  98. )
  99. def render_page_content(pathname, search):
  100. if pathname in ["/", "/overview"]:
  101. return overview_layout
  102. elif pathname == "/deepdive":
  103. if search:
  104. query_params = parse_query_params(search)
  105. else:
  106. query_params = {}
  107. return create_deepdive_layout(**query_params)
  108. # If the user tries to reach a different page, return a 404 message
  109. return dbc.Jumbotron(
  110. [
  111. html.H1("404: Not found", className="text-danger"),
  112. html.Hr(),
  113. html.P(f"The pathname {pathname} was not recognised..."),
  114. ]
  115. )
  116.  
  117.  
  118. @app.callback(
  119. Output("deepdive-output", "children"), [Input("cluster-dropdown", "value")]
  120. )
  121. def create_cluster_output(value):
  122. if value:
  123. return f"Cluster {value} is selected"
  124. return "Nothing is selected..."
  125.  
  126.  
  127. if __name__ == "__main__":
  128. app.run_server(debug=True)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement