Noah
Noah

Reputation: 101

Get parameters from url in dash python: This ID is assigned to a dash_core_components.Location component in the layout,which does not support property

The following is my code:

I would like to get the parameters from the url and print it out in the webpage.

For example, http://127.0.0.1:8051/random?xaxis=log&yaxis=log is my url.

I want to print out the pathname in the webpage, while I get return as None now. Where am I going wrong.

import dash
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output

app = dash.Dash(__name__)

app.layout = html.Div([
    dcc.Location(id='url', refresh=False),
    html.Br(),
    html.Div(id='my-output')])


@app.callback(
    Output(component_id='my-output', component_property='children'),
    Input(component_id='url', component_property='value'))
def update_output_div(value):
    return html.Div([
        html.H1('path is {}'.format(str(value)))
    ])

if __name__ == '__main__':
    app.run_server(debug=True, port= 8051)

I get the output as

enter image description here

Upvotes: 5

Views: 5776

Answers (1)

Noah
Noah

Reputation: 101

Enter url http://127.0.0.1:8051/random?param1=cat&param2=dog in your browser

cat and dog will be displayed

import dash
from dash import dcc
from dash import html
from dash.dependencies import Input, Output
from furl import furl


app = dash.Dash(__name__)


app.config.suppress_callback_exceptions = True
app.layout = html.Div([
    # represents the URL bar, doesn't render anything
    dcc.Location(id='url', refresh=False),

    # content will be rendered in this element
    html.Div(id='content'),
])


@app.callback(Output('content', 'children'),
              [Input('url', 'href')])
def _content(href: str):
    f = furl(href)
    param1= f.args['param1']
    param2= f.args['param2']

    return html.H1(children=param1: {param1} param2: {param2}' )

if __name__ == '__main__':
    app.run_server(debug=True, port= 8051)

Upvotes: 5

Related Questions