How to access browser location url query string on server? #83
-
|
How would I access the http request querystring parameters on the server? Do we have access to an http request object for the current route? |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 6 replies
-
|
You can do something like that |
Beta Was this translation helpful? Give feedback.
-
|
@jourdain I can't import trame from widgets anymore. Is there an updated way of doing this? I'm trying to embedd a trame-application in a larger React-application, and getting hold of query-parameters/path-parameters is crucial for this setup to work. Edit: I hadn't installed trame-components, so I could find them now. I also have some trouble building an application that depend on the query parameters. E.g. for authentication and db-fetching. Could you help me figure out how to use the query parameters that is extracted in the get_search-method and render different pages depending on what's in the query-parameters? I would want to set up something like: from typing import Any
from trame.app import get_server
from trame.ui.vuetify3 import SinglePageLayout
from trame.widgets import trame
from trame_server.controller import Controller
from trame_server.state import State
server = get_server()
server.client_type = "vue3"
state: State = server.state
ctrl: Controller = server.controller
layout = SinglePageLayout(server)
@ctrl.set("get_search")
def get_search(search):
if search.startswith("?"):
search = search[1:]
query_list = search.split("&")
queries = [(item.split("=")[0], item.split("=")[1]) for item in query_list]
state.search = {
"token": [query_tuple[1] for query_tuple in queries if query_tuple[0] == "token"][0],
"foo_id": [query_tuple[1] for query_tuple in queries if query_tuple[0] == "foo_id"][0],
}
return state.search
def render_app(layout: SinglePageLayout):
pass
with layout:
trame.ClientTriggers(mounted=(ctrl.get_search, "[window.location.search]"))
if hasattr(state, "search") and isinstance(state.search, dict):
token = state.search.get("token", None)
foo_id = state.search.get("foo_id", None)
if token and foo_id:
render_app(layout)
elif token and not foo_id:
layout.title.set_text("Couldn't find foo")
elif not token and foo_id:
layout.title.set_text("Couldn't authenticate")
else:
layout.title.set_text("Couldn't authenticate")
else:
layout.title.set_text("Loading") |
Beta Was this translation helpful? Give feedback.
-
|
I solved it by putting the application inside the get_search, function. But I have the same problem as chriswallis. The line trame.ClientStateChange(value="window.location.search", trigger_on_create=True, change=(ctrl.get_search, "[window.location.search]"))Doesn't seem to do anything when updating the url parameters. I need the server to respond to changing requests, so it must be able to updating on a change in query parameters |
Beta Was this translation helpful? Give feedback.
-
Beta Was this translation helpful? Give feedback.

You can do something like that