h2oai / wave

Realtime Web Apps and Dashboards for Python and R
https://wave.h2o.ai
Apache License 2.0
3.9k stars 324 forks source link

Support reading query URL params #2083

Open daffyduck25 opened 11 months ago

daffyduck25 commented 11 months ago

Is your feature request related to a problem? Please describe

Would like to pass key/var pairs into the app.

(A clear and concise description of what the problem is. Ex. I'm always frustrated when [...])

Describe the solution you'd like

Would like to pass URL query parameters into the app. For example: h20-wave-app-url?key1=var1,key2=var2, etc...

(A clear and concise description of what you want to happen.)

We could then read the vars in the app. Ex something similar to this: https://www.w3docs.com/snippets/php/get-url-query-string-parameters.html#:~:text=To%20get%20the%20query%20string,of%20the%20query%20string%20parameters.&text=Note%20that%20if%20the%20parameter,if%20a%20parameter%20is%20set.

Describe alternatives you've considered

The hash works to pass a string into the app however it must be delimited for multiple values and cannot be assigned keys. Ex. h2o-wave-app-url#somevariable,someothervariable, ...

(A clear and concise description of any alternative solutions or features you've considered.)

Additional context

Add any other context or screenshots about the feature request here.

mturoci commented 11 months ago

Code to get the URL params today (until this feature is implemented):

from h2o_wave import Q, ui, main, app

js_script = '''
const queryParams = new URLSearchParams(window.location.search)
const params = Object.fromEntries(queryParams.entries())
wave.emit('load', 'url_params', params)
'''

@app('/')
async def serve(q: Q):
    if not q.client.initialized:
        q.page['meta'] = ui.meta_card(box='', script=ui.inline_script(content=js_script))
        q.client.initialized = True

    if q.events.load:
        q.page['params'] = ui.markdown_card(
            box='1 1 3 3',
            title='URL params',
            content=str(q.events.load.url_params)
        )

    await q.page.save()

Go to http://localhost:10101/?foo=bar for example and you should see:

image