Hello World in Flask

- Andrés Cruz

En español
Hello World in Flask

Flask is a great web framework for creating server-side web applications with Python; if you do not have knowledge of using server web frameworks with Python, Flask is a good candidate to start with; being a microframework, which means that initially it comes with the minimum necessary to be able to function, you can see little by little how the framework works without giving rise to confusion with files, directory structure or modules that you have not placed, as happens with others. more robust frameworks like Django, which when creating a project in Django, has many files and folders.

Finally, it's time to create the "Hello World" with Flask; that is, to implement the minimum necessary to be able to see something on the screen; so, we create a file inside the project (the tasks folder):

main.py

With the following code:

main.py

from flask import Flask 
app = Flask(__name__) 
 
@app.route('/') 
def hello_world() -> str: 
    return 'Hello Flask!' 

if __name__ == '__main__': 
    app.run() 

The previous code is a complete web application based on Flask, from here we can understand the term microframework that denotes Flask, since it has the minimum necessary to be able to work, therefore, a minimal implementation in Flask can be written in a few lines of code; the old app will simply display "Hello Flask!" in the browser.

We raise the application with:

$ python main.py

And in the terminal we will see something like:

 * Serving Flask app 'main'
 * Debug mode: on
WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead.
 * Running on http://127.0.0.1:5000

Now, if we navigate to the previous route:

http://127.0.0.1:5000. 

We will see a message from

Hello Flask!

On the screen.

Let's take a closer look at the above code; through:

from flask import Flask 
app = Flask(__name__) 

We import the Flask class, which is used to create an instance, the so-called app, and from which you can perform multiple configurations at the project level, such as the database, sending emails, routing, among others, but, for now, only is used to create a route:

@app.route('/') 

This decorator is used in the function that it decorates can be consumed from an HTTP request through the associated route; in this example, the path '/' is assigned to the view function hello_world().

When a user accesses the '/' path, Flask will call the hello_world() function and return the string "Hello Flask!".

Finally, the application is executed with:

if __name__ == '__main__': 
    app.run() 

The above conditional checks if the current module is the main program that is being executed. If so, the code inside the if code block is executed; that allows starting the web server that Flask uses internally: Werkzeug.

The if __name__ == '__main__' conditional: ensures that the server is only executed if the script is executed directly from the Python interpreter and not from an imported module.

You can try returning HTML content instead of text like:

main.py

@app.route('/') 
def hello_world(): 
    return '<h1>Hello World!</h1>'
Andrés Cruz

Desarrollo con Laravel, Django, Flask, CodeIgniter, HTML5, CSS3, MySQL, JavaScript, Vue, Android, iOS, Flutter

Andrés Cruz en Udemy

Acepto recibir anuncios de interes sobre este Blog.