Зміст курсу
Flask Intensive Course: Web Development with Python
Flask Intensive Course: Web Development with Python
Basic Flask Application
Install Flask
Let's start our application. First of all, we need to install Flask. You can use Terminal to run the command.
You may receive this message: Successfully installed.
If you receive: [notice] To update, run: pip install --upgrade pip.
Keep in mind that it's generally a good practice to keep your development tools, including pip, up to date to ensure you're using the latest and most secure versions of packages and to prevent potential compatibility issues.
You can use the proposed command:
Let's check the installed Flask version by:
You must receive a similar message to this:
Build Flask app
Let's go further and write our first app. In the previous step, we have created an empty main.py file. Go to Editor Window of the main.py file.
When clients like web browsers send requests to the web server, it directs those requests to the Flask app. To figure out which code to run for each URL requested, the app keeps a list that matches URLs with Python functions. This link between a URL and its handling function is known as a route.
This special part of our application helps us to run the file directly and prevent parts of the code from being run when the module is imported to another package or module. Running this file directly is always a good practice. You can easily write main and click Tab on your keyboard, and you will receive if __name__ == "__main__"
.
This hack may not work in the VScode editor.
Run application
You can run this file by calling:
In the Terminal window or by clicking the run button in the header of the PyCharm window.
In the Terminal window, we see the URL where the number part before :
the colon symbol is our domain. It is a local domain, and while we convenient to see global domains like google.com or others, this local domain is useful during development. The 4 numbers after the colon is our port.
IP address or localhost is used to identify the computer, and Port is used to identify the software such as our app, Chrome, Skype, and any other applications.
The port number can change during development because other programs might be using the same port, and the runner may select an alternative port for running. In our case, it is 5000 or 8000.
By clicking on the domain http://127.0.0.1:5000
or http://127.0.0.1:8000
, it forwards us to the web page on your default browser. And we see our message. Congratulations, you created your first website by Flask!
Further, we will cover the design with HTML templates, CSS, and Bootstrap, and how to get parameters from URL to our code, we will use Databases to store and retrieve stored data.
If you enter a different URL, the app won't understand what to do and will send a 404 error to your browser. This is the error you see when a webpage doesn't exist.
Дякуємо за ваш відгук!