picoweb redirect

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
mattzz
Posts: 5
Joined: Tue Nov 13, 2018 7:50 pm

picoweb redirect

Post by mattzz » Tue Nov 13, 2018 7:59 pm

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

OutoftheBOTS_
Posts: 847
Joined: Mon Nov 20, 2017 10:18 am

Re: picoweb redirect

Post by OutoftheBOTS_ » Wed Nov 14, 2018 10:49 am

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

pfalcon
Posts: 1155
Joined: Fri Feb 28, 2014 2:05 pm

Re: picoweb redirect

Post by pfalcon » Fri Nov 16, 2018 7:48 pm

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.
Awesome MicroPython list
Pycopy - A better MicroPython https://github.com/pfalcon/micropython
MicroPython standard library for all ports and forks - https://github.com/pfalcon/micropython-lib
More up to date docs - http://pycopy.readthedocs.io/

mattzz
Posts: 5
Joined: Tue Nov 13, 2018 7:50 pm

Re: picoweb redirect

Post by mattzz » Sun Nov 18, 2018 10:52 am

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)

pfalcon
Posts: 1155
Joined: Fri Feb 28, 2014 2:05 pm

Re: picoweb redirect

Post by pfalcon » Tue Nov 20, 2018 6:48 am

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).
Awesome MicroPython list
Pycopy - A better MicroPython https://github.com/pfalcon/micropython
MicroPython standard library for all ports and forks - https://github.com/pfalcon/micropython-lib
More up to date docs - http://pycopy.readthedocs.io/

Post Reply