DEV Community

Cover image for Return variables with Flask
bluepaperbirds
bluepaperbirds

Posted on

Return variables with Flask

Python can be used for web development, Flask is a Python web framework based on WSGI. Flask lets you build web apps with Python (what is Flask).

You can use Flask to build single page apps, a blog, web apps and many more things. Some people use it to build a user interface on their Raspberry Pi.

Before playing with Flask, you should learn Python

Return variables in Flask

There are different ways to return variables with Flask. Did you know you can use f-Strings inside Flask? This is one of the most simple ways.

If you want to return some variables for your request, you can do:

return f'a is {a} and b is {b}'

So a full example of returning these two variables would be

from flask import Flask

app = Flask(__name__)

@app.route('/')
def index():
    a = 62
    b = 41
    return f'a is {a} and b is {b}'

app.run(host='0.0.0.0', port=5000)

In the browser:

flask variables

Run the server and open the localhost url on your pc:

➜  ~ python3 example.py
 * Serving Flask app "example" (lazy loading)
 * Environment: production
   WARNING: This is a development server. Do not use it in a production deployment.
   Use a production WSGI server instead.
 * Debug mode: off
 * Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)

Related links:

Top comments (0)