Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Flask Basics Welcome to Flask Clean URL Arguments

Wu Ray
Wu Ray
2,359 Points

Why we still have to str(<value> from flask input) to return value?

Hello guys, I don't understand. if we make an adding value application in our app.route like: app.route('/add/<int:num1>/<int:num2>'),

isn't this mean we make the flask input ( as default string) to return to us as an int value? so we can do: return "{}+{}={}".format(num1, num2, num1+num2).

why if we want to: return num1+num2 will cause error?

Are not they have turned into int value already? thanks for helping~

3 Answers

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,423 Points

I hope I'm understanding your question correctly.

The return value can not simply be a number. A string or properly formed response object is expected. See docs About Responses (scroll down)

About Responses

The return value from a view function is automatically converted into a response object for you. If the return value is a string it’s converted into a response object with the string as response body, an 200 OK error code and a text/html mimetype. The logic that Flask applies to converting return values into response objects is as follows:

  • If a response object of the correct type is returned it’s directly returned from the view.
  • If it’s a string, a response object is created with that data and the default parameters.
  • If a tuple is returned the items in the tuple can provide extra information. Such tuples have to be in the form (response, status, headers) where at least one item has to be in the tuple. The status value will override the status code and headers can be a list or dictionary of additional header values.
  • If none of that works, Flask will assume the return value is a valid WSGI application and convert that into a response object.

Post back if I misunderstood.

Wu Ray
Wu Ray
2,359 Points

Thanks Chris for posting this document for my reference. so for everything we throw into flask, it will return value which is "A string or properly formed response object is expected"?
when my python and html are communicating, we only can use string? if we want to change the type, we only do that after variables are sent to python or flask?

Chris Freeman
Chris Freeman
Treehouse Moderator 68,423 Points

One strength of flask is it can create the proper response object to send back to the browser for you. This includes any HTML content along with Responce Codes (200, 400, ...)..

In the end, all communication between the browser and the app is text-based. Once the text objects are received at either end, then it can be converted as needed for local proposes.

Wu Ray
Wu Ray
2,359 Points

Thanks Chris, you really help me a lot!