Skip to content

Instantly share code, notes, and snippets.

@Akronix
Created September 28, 2018 13:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Akronix/998dea99be457584e8393d437935b2ee to your computer and use it in GitHub Desktop.
Save Akronix/998dea99be457584e8393d437935b2ee to your computer and use it in GitHub Desktop.
Dash: Shows dcc.Location atributes available in dcc.Link
import dash
import dash_core_components as dcc
import dash_html_components as html
print(dcc.__version__) # 0.6.0 or above is required
external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css']
app = dash.Dash(__name__, external_stylesheets=external_stylesheets)
app.layout = html.Div([
# represents the URL bar, doesn't render anything
dcc.Location(id='url', refresh=False),
dcc.Link('Navigate to "/"', href='/'),
html.Br(),
dcc.Link('Navigate to "/page-2"', href='/page-2'),
# content will be rendered in this element
html.Div(id='href'),
html.Div(id='page-content'),
html.Div(id='qs'),
html.Div(id='hash')
])
@app.callback(dash.dependencies.Output('page-content', 'children'),
[dash.dependencies.Input('url', 'pathname')])
def display_page(pathname):
return html.Div([
html.H3('You are on page {}'.format(pathname))
])
@app.callback(dash.dependencies.Output('href', 'children'),
[dash.dependencies.Input('url', 'href')])
def display_page(href):
return html.Div([
html.H3('This is the href: {}'.format(href))
])
@app.callback(dash.dependencies.Output('qs', 'children'),
[dash.dependencies.Input('url', 'search')])
def display_page(qs):
# Surprinsingly, this callback is called with search = None
# before to be called again with the actual url :S
print ('This is the query string: {}'.format(qs))
# This callback should not be called when the query string of the url isnt affected!!!
return html.Div([
html.H3('This is the query string: {}'.format(qs))
])
# Same here, this callback gets triggered every time with first hash=None and
# then hash = actual value and the callback gets called everytime the path
# changes, also when the *hash* part hasn't been modified.
@app.callback(dash.dependencies.Output('hash', 'children'),
[dash.dependencies.Input('url', 'hash')])
def display_page(hash_url):
print ('This is the url hash: {}'.format(hash_url))
return html.Div([
html.H3('This is the url hash: {}'.format(hash_url))
])
if __name__ == '__main__':
app.run_server(debug=True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment