Basics of web development

Python and Flask

Easy
15 min

Let's start by introducing Flask, as we will be using it as an example in all the modules of this section. If you are not familiar with Python programming yet, take the Hakatemian Python programming course before continuing from here.

What is Flask?

Flask is a lightweight and flexible web framework designed to facilitate the development of web applications using the Python programming language. Flask provides a foundation for creating web applications that are easy to understand and maintain. It is a popular choice among many developers as it offers many features that make web development fast and efficient. Flask is also an open-source project, which means that developers can benefit from the contributions of the community and share their experiences with others. This makes Flask a very user-friendly framework suitable for both beginners and experienced developers.

Simple example

Here is a simple Flask program:

from flask import Flask app = Flask(__name__) @app.route(&#39;/&#39;) def hello_world(): return &#39;<h1> Hello, Hakatemia!</h1> &#39; app.run(host=&#39;0.0.0.0&#39;, port=81)


This program creates a web application that displays the "Hello, Hakatemia!" heading when the user visits the root directory of the application.

The first line of the program loads Flask. The next line creates a new instance of the Flask application named "app". __name__ tells Flask how to find the application in the file structure, you don't need to worry about that now.

The first line of the code determines the initial route ("/") of the application using the @app.route decorator. This decorator tells Flask that the function handles HTTP requests with the path / (in practice, the homepage).

On the fourth line, the hello_world function is defined, which returns an HTML document:

<h1>Hello, Hakatemia!</h1>

Finally, the Flask development server is started, which listens on port 81.

Practice

Kokeile itse!

Valitettavasti Replit-palvelu on muuttnut lennosta eikä enää anna suorittaa näitä koodeja suoraan selaimessa. Voit klikata alla olevaa "Open in Replit" linkkiä ja avata koodin Replit-palvelussa. Meillä on työn alla etsiä Replitille korvaaja.

hakatemia pro

Ready to become an ethical hacker?
Start today.

As a member of Hakatemia you get unlimited access to Hakatemia modules, exercises and tools, and you get access to the Hakatemia Discord channel where you can ask for help from both instructors and other Hakatemia members.