Home > Backend Development > Python Tutorial > Ignite with Flask

Ignite with Flask

Linda Hamilton
Release: 2025-01-20 02:12:09
Original
676 people have browsed it

Ignite with Flask

Getting Started Guide: Quickly Getting Started with the Flask Framework

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!

What is Flask?

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.

Why choose Flask framework?

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.

1. Lightweight and minimalist:

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.

2. Easy to learn for beginners:

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.

3. Flexibility and customizability:

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.

  • Commonly used libraries or Flask extensions include:
    • Flask-SQLAlchemy for database integration
    • Flask-WTF for form validation
    • Flask-Login for user authentication

4. Built-in development server and debugger:

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.

5. Very suitable for prototype development:

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.

Quick Start with Flask

Prerequisites:

Before starting to use Flask, we need to ensure the following:

  1. 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>
    Copy after login
    Copy after login
    Copy after login

(If Python is not installed, just search Python.org on Google to resolve this issue)

  1. 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>
    Copy after login
    Copy after login
    Copy after login
  2. Code Editor: Write Flask code using the code editor of your choice. My personal preference is VSCode.

  3. Terminal or command line: used to run Flask applications.

Flask setup step-by-step guide

  1. Set project directory:

Create a new Flask project directory and navigate to the project using your terminal:

<code class="language-bash"> pip --version</code>
Copy after login
  1. Create virtual environment:

Virtual environments help isolate project dependencies. Run the following code:

<code class="language-bash">mkdir flask_blog_project
cd flask_blog_project</code>
Copy after login

This will create a virtual environment called venv.

  • Activate virtual environment:

    • On macOS/Linux:

      <code class="language-bash">python -m venv venv</code>
      Copy after login
    • On Windows:

      <code class="language-bash">  source venv/bin/activate</code>
      Copy after login

Your terminal should start with (venv), which means the environment is activated.

  1. Install Flask:

Install Flask using pip in an activated virtual environment:

<code class="language-bash">  venv\Scripts\activate</code>
Copy after login

Verify installation:

<code class="language-bash">pip install flask</code>
Copy after login
  1. Create a basic Flask application:

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>
Copy after login
  1. Run Flask application:

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>
Copy after login
  1. Start the Flask development server:

    <code class="language-bash"># macOS/Linux:
    export FLASK_APP=app
    
    # Windows (命令提示符):
    set FLASK_APP=app</code>
    Copy after login
  2. Open your browser and visit //m.sbmmt.com/link/455a9ce9e0c8e8a0c7745eecd6a64be4

    <code class="language-bash"> flask run</code>
    Copy after login
  3. Add more routes:

To make your application more dynamic, add more routes. Update your app.py:

<code> Hello, 博客读者!欢迎来到我的 Flask 应用。</code>
Copy after login
  1. Learn templates and static files:

Flask supports dynamic HTML rendering using the Jinja2 template engine. For example:

  • Create a folder called templates in your project directory.
  • In 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>
Copy after login
  • Update 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>
Copy after login
  • 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>
    Copy after login
  1. Exploring Flask Extensions:

Flask has many extensions to help you add functionality. Some commonly used extensions include:

  • Flask-SQLAlchemy: for database integration.
  • Flask-Login: used for user authentication.
  • Flask-WTF: used for form processing.

Use pip to install the extension, for example:

<code class="language-bash"> python --version</code>
Copy after login
Copy after login
Copy after login

The above is the detailed content of Ignite with Flask. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template