New to Python? Want to get started quickly and create your first project? Python is a powerful language that can help you build a variety of applications, from automation tools to web applications. This article will take you step by step to learn the Flask framework, covering installation, configuration, and building your first project. Let's get started!
Flask is a web framework written in Python that provides the key backend components needed to build web applications. Flask is a lightweight "micro" framework because it focuses on providing the core functionality of web development and avoids unnecessary components, thereby improving customization and performance. If your priorities are simplicity and control, Flask is ideal.
Now that we understand the basic concepts of Flask, let’s take a look at why beginners like you and me should choose it and its key features.
As mentioned earlier, Flask is a micro web framework. It provides only the tools needed to run your application without adding rigid or unnecessary components. This is ideal for small to medium-sized applications and projects.
Flask’s design is simple and clear, making it easier for beginners to master the basics of Flask and web development. Flask has excellent official documentation with examples and guides for beginners.
Flask, unlike other frameworks, allows you to use libraries and extensions of your choice. You are free to decide how to organize your code, handle databases, manage user authentication, etc.
Another powerful feature of Flask is the built-in development server and debugger, which makes testing and troubleshooting web applications easier. The debugger allows developers to inspect returned errors directly in the browser.
Since Flask is a lightweight framework, it is ideal for building prototypes and minimum viable products (MVPs). It allows you to quickly test ideas without being hampered by heavyweight frameworks.
Before starting to use Flask, we need to ensure the following:
Python installed: Flask is a Python framework, and you cannot use Flask without Python.
Run the following command to check if Python is installed:
<code class="language-bash"> python --version</code>
(If Python is not installed, just search Python.org on Google to resolve this issue)
Pip (Python Package Installer) : Pip is usually pre-installed with Python, but you should always check and verify by running the following command:
<code class="language-bash"> python --version</code>
Code Editor: Write Flask code using the code editor of your choice. My personal preference is VSCode.
Terminal or command line: used to run Flask applications.
Create a new Flask project directory and navigate to the project using your terminal:
<code class="language-bash"> pip --version</code>
Virtual environments help isolate project dependencies. Run the following code:
<code class="language-bash">mkdir flask_blog_project cd flask_blog_project</code>
This will create a virtual environment called venv.
Activate virtual environment:
On macOS/Linux:
<code class="language-bash">python -m venv venv</code>
On Windows:
<code class="language-bash"> source venv/bin/activate</code>
Your terminal should start with (venv), which means the environment is activated.
Install Flask using pip in an activated virtual environment:
<code class="language-bash"> venv\Scripts\activate</code>
Verify installation:
<code class="language-bash">pip install flask</code>
In your project folder, create a new file called app.py with the following code:
<code class="language-bash">python -c 'import flask; print(flask.__version__)'</code>
Set FLASK_APP environment variable:
<code class="language-python">from flask import Flask app = Flask(__name__) @app.route('/') def home(): return "Hello, 博客读者!欢迎来到我的 Flask 应用。" if __name__ == '__main__': app.run(debug=True)</code>
Start the Flask development server:
<code class="language-bash"># macOS/Linux: export FLASK_APP=app # Windows (命令提示符): set FLASK_APP=app</code>
Open your browser and visit //m.sbmmt.com/link/455a9ce9e0c8e8a0c7745eecd6a64be4:
<code class="language-bash"> flask run</code>
Add more routes:
To make your application more dynamic, add more routes. Update your app.py:
<code> Hello, 博客读者!欢迎来到我的 Flask 应用。</code>
flask run
to restart). Flask supports dynamic HTML rendering using the Jinja2 template engine. For example:
templates
in your project directory. templates
, create a index.html
file: <code class="language-python">@app.route('/about') def about(): return "您正在访问关于页面。" @app.route('/greet/<name>') def greet(name): return f"您好,{name.capitalize()}!"</code>
app.py
to render template: <code class="language-html"><!DOCTYPE html> <html> <head> <title>Flask App</title> </head> <body> <h1>欢迎来到我的应用程序,{{ name }}!</h1> </body> </html></code>
Restart the Flask server and visit:
<code class="language-python">from flask import render_template @app.route('/welcome/<name>') def welcome(name): return render_template('index.html', name=name)</code>
Flask has many extensions to help you add functionality. Some commonly used extensions include:
Use pip to install the extension, for example:
<code class="language-bash"> python --version</code>
The above is the detailed content of Ignite with Flask. For more information, please follow other related articles on the PHP Chinese website!