librelist archives

« back to archive

How to convert this Bottle code to Flask?

How to convert this Bottle code to Flask?

From:
Andrew Stuart (SuperCoders)
Date:
2012-06-19 @ 13:49
I am migrating from Bottle to Flask.

Unfortunately Flask is not available for Python 3, so I now also need  
to downgrade to Python 2 - as a newbie to Python I really did not want  
to have to learn the old version of Python but it seems all the Python  
greybeards are dragging their feet on moving to the new.......

Anyway enough grumbling about Python politics.

In Bottle for Python 3, this code echoes POST data back to the  
browser.  I'm darned if I can get the equivalent to work using Flask  
and Python 2.  Any advice valued.

# echos POST data back to client
@bottle.route('/echopost', method='POST')
def echopost():
     data = bottle.request.body.read().decode('utf-8')
     print(data)
     if not bottle.request.body:
         abort(400, 'No data received')
     return data

Re: [flask] How to convert this Bottle code to Flask?

From:
Simon Sapin
Date:
2012-06-19 @ 14:13
Le 19/06/2012 15:49, Andrew Stuart (SuperCoders) a écrit :
> I am migrating from Bottle to Flask.
>
> Unfortunately Flask is not available for Python 3, so I now also need
> to downgrade to Python 2 - as a newbie to Python I really did not want
> to have to learn the old version of Python but it seems all the Python
> greybeards are dragging their feet on moving to the new.......
>
> Anyway enough grumbling about Python politics.
>
> In Bottle for Python 3, this code echoes POST data back to the
> browser.  I'm darned if I can get the equivalent to work using Flask
> and Python 2.  Any advice valued.
>
> # echos POST data back to client
> @bottle.route('/echopost', method='POST')
> def echopost():
>       data = bottle.request.body.read().decode('utf-8')
>       print(data)
>       if not bottle.request.body:
>           abort(400, 'No data received')
>       return data

Hi,

I wonder how Armin feels being called a greybeard dragging his feet, but 
let’s leave it at that :)

What specific problem do you have?

The docs are here: http://flask.pocoo.org/docs/

The quick start should have almost everything you need.
(Hint: `methods=['POST']` vs. `method='POST'`)
Other specific points are in the API docs:

http://flask.pocoo.org/docs/api/#flask.request
http://flask.pocoo.org/docs/api/#flask.Request.data
http://flask.pocoo.org/docs/api/#flask.abort

-- 
Simon Sapin

Re: [flask] How to convert this Bottle code to Flask?

From:
Andrew Stuart
Date:
2012-06-19 @ 22:26
"Greybeards of Python" is a metaphor for "wise old men of Python".   
It's a compliment. I know Armin is young and I greatly admire his work  
- clearly genius level intellect. I read his blog post on Python 3 and  
sympathise with his thoughts about it.

For the Python noob though (i.e. me) however it just doesn't make  
sense to learn the idiosyncrasies of Python 2.  To the newcomer,  
Python 3 looks like a much better proposition - the consistent  
approach to unicode alone make it appealing.  It's just frustrating  
that more Python stuff hasn't moved to Python 3 when it is now 3.5  
years since it was released.  There has to be a tipping point doesn't  
there when people start to see Python 2 code as "legacy"?  Fortunately  
I have found many projects are now supporting Python 3 (some more  
thoroughly than others).

Anyway please disregard my original question.  I'm going to try to  
stick with bottle despite my preference for the features and  
documentation of flask.  I really want to avoid learning Python 2 - I  
just don't have time.

I'll move to flask with joy when it's available on Python 3.

A

On 20/06/2012, at 12:13 AM, Simon Sapin wrote:

Le 19/06/2012 15:49, Andrew Stuart (SuperCoders) a écrit :
> I am migrating from Bottle to Flask.
>
> Unfortunately Flask is not available for Python 3, so I now also need
> to downgrade to Python 2 - as a newbie to Python I really did not want
> to have to learn the old version of Python but it seems all the Python
> greybeards are dragging their feet on moving to the new.......
>
> Anyway enough grumbling about Python politics.
>
> In Bottle for Python 3, this code echoes POST data back to the
> browser.  I'm darned if I can get the equivalent to work using Flask
> and Python 2.  Any advice valued.
>
> # echos POST data back to client
> @bottle.route('/echopost', method='POST')
> def echopost():
>      data = bottle.request.body.read().decode('utf-8')
>      print(data)
>      if not bottle.request.body:
>          abort(400, 'No data received')
>      return data

Hi,

I wonder how Armin feels being called a greybeard dragging his feet, but
let’s leave it at that :)

What specific problem do you have?

The docs are here: http://flask.pocoo.org/docs/

The quick start should have almost everything you need.
(Hint: `methods=['POST']` vs. `method='POST'`)
Other specific points are in the API docs:

http://flask.pocoo.org/docs/api/#flask.request
http://flask.pocoo.org/docs/api/#flask.Request.data
http://flask.pocoo.org/docs/api/#flask.abort

-- 
Simon Sapin
-- 
Message  protected by MailGuard: e-mail anti-virus, anti-spam and  
content filtering.http://www.mailguard.com.au/mg
Click here to report this message as spam:
https://login.mailguard.com.au/report/1EYoWykSES/3V0RJoSW6aEQPBY2PjwJ83/0

Re: [flask] How to convert this Bottle code to Flask?

From:
pronoyc@gmail.com
Date:
2012-06-20 @ 05:20
>
> > # echos POST data back to client
> > @bottle.route('/echopost', method='POST')
> > def echopost():
> >      data = bottle.request.body.read().decode('utf-8')
> >      print(data)
> >      if not bottle.request.body:
> >          abort(400, 'No data received')
> >      return data
>

Returning POST data back to the browser is easy. On request.method ==
"POST" get the form elements and put them in a dict and show them on the
template.

@app.route('/foo', methods=["GET","POST"])def sendbackdata():
    """docstring for sendbackdata"""
    if request.method == "POST":
        some_var1 = request.form['some_random_element_name1']
        some_var2 = request.form['some_random_element_name2']
        some_var3 = request.form['some_random_element_name3']

        some_dict = {'foo1':some_var1, 'foo2': some_var2, 'foo3': some_var3}
    return render_template("template.html", some_dict=some_dict)


Then it's simply the matter of printing the elements in the template which
is trivial. As Simon mentioned, the docs have it all. Good luck.


-- 
Regards,

Pronoy Chopra
http://blog.pronoy.in <http://www.pronoy.in/about>/
http://www.twitter.com/pronoyc