Kitware / trame

Trame lets you weave various components and technologies into a Web Application solely written in Python.
https://kitware.github.io/trame/
Other
417 stars 54 forks source link

forward query to _ws_handler #609

Open HelgeGehring opened 1 week ago

HelgeGehring commented 1 week ago

I'd like to load different files based on the query string

Modiefied part of https://github.com/Kitware/trame/blob/master/trame/tools/serve.py :

    async def _index_handler(self, request):
        if request.query_string:
            print(request.query)
            return aiohttp.web.HTTPFound(f"index.html?{request.query_string}")
        return aiohttp.web.HTTPFound("index.html")

    async def _ws_handler(self, request):
            print(request.query)

The first print prints the query, the second doesn't. I'd need the information in the second part to use it for initializing the app, right?

Describe the solution you'd like

In the index-.....js i find

xr=`${window.location.protocol==="https:"?"wss:":"ws:"}//${window.location.hostname}:${window.location.port}${mr("/ws")}`;

I'd guess if we would add there the query string, it should be forwarded such the second print would also print it?

Describe alternatives you've considered

What I do right now as a workaround:

...
        self.queries = []

    async def _index_handler(self, request):
        if request.query_string:
            self.queries.append(request.query_string)
            return aiohttp.web.HTTPFound(f"index.html?{request.query_string}")
        return aiohttp.web.HTTPFound("index.html")

    async def _ws_handler(self, request):
        print(self.queries.pop())

Does more or less the job, but it's for sure not a solution

jourdain commented 1 week ago

You have several paths to handle it.

  1. Doing at startup: when you deploy your application with docker, the url args can be used in the args of the application. (see discussion)
  2. Doing on the client: When the client connect, it will call a method on the server to do something about the URL. (see discussion)
HelgeGehring commented 1 week ago

Thanks for the quick reply!

I'll have a look at 1!

  1. doesn't really seem to me a solution for my case, as I'd like to have things loaded before showing the gui.

would there be a reason to not just forward the parameters? return aiohttp.web.HTTPFound(f"index.html?{request.query_string}") already forwards it to the html-page such that i think it would be just a simple addition to the .js file to forward it to /ws?

Thanks!

jourdain commented 1 week ago

trame.tools.serve is not meant to be used in real production especially when VTK/ParaView is involved. If trame is used for doing dashboards then that runner could make sense. But either way, we are more on a process based separation than a async task based separation. So that url arg is already taking care at the launcher stage.