Query strings and global session state

Hello. I would like to be able to access a query string within my app, and then store it in a session state, such that all of my functions can access it.

Example: I would like to open http://localhost:8000/?pizza=margarita, and then store "margarita" with a session variable, such that all my functions know that we are dealing with a margarita.

I have found some relevant information for R (getQueryString), but so far nothing for Python. I have inspected the server()'s session.http_conn object and all of its attributes, but unfortunately I can't find anything.

I suppose there are two questions: 1) how can I access a query string (pizza=margarita) and 2) what is the best way to store a session variable in Shiny for Python?

I will let the others to come up with a better solution but my hacked version looks something like below. All the information is embedded in http header. This would only work on a server and not when you run this through the IDE

from shiny import App, Inputs, Outputs, Session, render, ui

app_ui = ui.page_fluid(
    ui.h3("HTTP request headers"),
    ui.output_text_verbatim("headers", placeholder=True),
    ui.h3("User and groups"),
    ui.output_text_verbatim("user_groups", placeholder=True),
)


def server(input: Inputs, output: Outputs, session: Session):
    @output
    @render.text
    def headers():
        s = ""
        for key, value in session.http_conn.headers.items():
            if key=="referer":
                 out=value.split("?")
                 out=out[1].split("=")
                 s=f'{out[0]}-{out[1]}'
                 
        return s

    @output
    @render.text
    def user_groups():
        return f"session.user: {session.user}\nsession.groups: {session. Groups}"
app = App(app_ui, server)

This topic was automatically closed 54 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.