Flask is a lightweight, versatile Python micro-framework for web applications that offers essential components such as URL routing and page rendering, making it a fantastic choice for developers looking to build efficient web applications with ease.
To begin learning Flask, I chose Visual Studio Code as my IDE and installed the Python extension from the Visual Studio Marketplace. It's important to note that I was using Python version 3.8.10 for this tutorial.
After installation, I created a new project folder called "flask-service-1" and opened it in VSCode. From the menu, I navigated to View - Command Palette (Ctrl+Shift+P) and selected "Python: Create Environment" to create a virtual environment separate from the global Python environment. I chose "venv" from the available options (the other choice being "Conda") and selected Python 3.8.10 64-bit as my desired version.
A new folder called ".venv" was created under the root folder of my project. Next, I ran "Terminal: Create New Terminal" (ctrl+shift+`) from the command palette, which opened a new terminal with the virtual environment automatically activated. To confirm the activation, the command prompt displayed (.venv) in front of it. To activate the virtual environment in an open terminal without activation, simply run ".venv/bin/activate" (and to deactivate, run ".venv/bin/deactivate").
With the virtual environment activated, I installed Flask by typing the following command in the terminal: python -m pip install flask. This process installed Flask along with several other essential packages.
Next, I created a new file called "app.py" in the project root folder and pasted the following code:
`
from flask import Flask
app = Flask(_name_)
@app.route("/")
def home():
return "Hello, World!"
`
In the terminal window, I executed the following command: python -m flask run. This produced the output:
`
- 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://127.0.0.1:5000/ (Press CTRL+C to quit)
`
Finally, I entered the URL http://127.0.0.1:5000 in my browser, which displayed the message: "Hello, World!"
This brief tutorial demonstrates the simplicity and efficiency of Flask, a Python micro-framework that makes web application development accessible and enjoyable for developers. With a streamlined setup process and the support of a virtual environment, Flask allows you to focus on creating quality web applications without getting bogged down in the complexities of larger frameworks. Give Flask a try and experience the power and flexibility it offers in the realm of web application development.
Top comments (0)