Page 1 of 1

picoweb redirect

Posted: Tue Nov 13, 2018 7:59 pm
by mattzz
Hi,

having used flask a bit I was wondering what the best way in picoweb was to redirect from one route to another.
Let's say I'm using a certain GET method on route '/switch' to toggle a pin and I want to return to '/index' after that.

Thanks,
/Matthias

Re: picoweb redirect

Posted: Wed Nov 14, 2018 10:49 am
by OutoftheBOTS_
Have you looked at this webserver for micro-python it is what I use for my projects https://github.com/jczic/MicroWebSrv
Here's a YouTube video made by a user on this forum https://www.youtube.com/watch?v=xscBwC1SrF4

Re: picoweb redirect

Posted: Fri Nov 16, 2018 7:48 pm
by pfalcon
mattzz wrote:
Tue Nov 13, 2018 7:59 pm
having used flask a bit I was wondering what the best way in picoweb was to redirect from one route to another.
picoweb is just a thin layer on top of HTTP. So the way to redirect in picoweb is the same as in HTTP. Just use your knowledge of how to do HTTP redirects, voila. As for the best way - well, everyone has their own best way, and it usually differs on a case by case basis.

Re: picoweb redirect

Posted: Sun Nov 18, 2018 10:52 am
by mattzz
OK, thanks guys.
Here's what I've ended up using:

Code: Select all

@app.route('/some_url')
def some_url(req, resp):
   ...
   (GET/POST parameter handling)
   ...
    # redirect to "/"
    headers = {"Location": "/"}
    yield from picoweb.start_response(resp, status="303", headers=headers)

Re: picoweb redirect

Posted: Tue Nov 20, 2018 6:48 am
by pfalcon
Righto. Can optimize it beyond that (or not - that's the power of picoweb: it gives a choice whether to optimize even such minor things, or forget about it and go pretty high-level). I'll consider adding a builtin func for that, thanks for ping. But the point is that you don't need to depend on that - you can do anything yourself, and picoweb is intended to stay minimal (and thus run on devices like esp8266 or even smaller!). Any (considerable) extension is supposed to come in the form of external modules/plugins (and that's not a surprise - Flask, the picoweb's inspiration, works in the same way).