1. Hello World Flask App:

This is the simplest Flask app that you can create. It displays a simple "Hello World!" message when the app runs.

Code:

from flask import Flask

app = Flask(__name__)

@app.route('/')
def hello_world():
    return 'Hello World!'
    
if __name__ == '__main__':
    app.run()

Explanation:

  • from flask import Flask: This line imports the Flask class from the flask module.
  • app = Flask(__name__): This creates a new Flask app instance.
  • @app.route('/'): This is a decorator that maps the URL "/" to the function hello_world().
  • def hello_world():: This is the function that returns the "Hello World!" message.
  • if __name__ == '__main__':: This is a standard Python check that ensures the app only runs when executed directly, not when imported as a module.
  • app.run(): This starts the Flask app.
  1. Displaying HTML:

This code shows how to display HTML content using Flask.

Code:

from flask import Flask

app = Flask(__name__)

@app.route('/')
def index():
    return '<h1>Hello, World!</h1>'

if __name__ == '__main__':
    app.run()

Explanation:

  • The index() function returns an HTML string, which is displayed in the browser when the app runs.
  1. Passing variables to HTML templates:

This code shows how to pass variables to an HTML template using Flask's render_template() method.

Code:

from flask import Flask, render_template

app = Flask(__name__)

@app.route('/')
def index():
    name = 'John'
    return render_template('index.html', name=name)

if __name__ == '__main__':
    app.run()

Explanation:

  • render_template() is a Flask method that allows you to render HTML templates.
  • The index() function passes the name variable to the index.html template using the render_template() method.
  1. Handling form data:

This code shows how to handle form data in Flask.

Code:

from flask import Flask, request

app = Flask(__name__)

@app.route('/')
def index():
    return '''
        <form method="POST" action="/hello">
            <label for="name">Enter your name:</label>
            <input type="text" id="name" name="name" required>
            <button type="submit">Submit</button>
        </form>
    '''

@app.route('/hello', methods=['POST'])
def hello():
    name = request.form['name']
    return f'Hello, {name}!'

if __name__ == '__main__':
    app.run()

Explanation:

  • The index() function displays a form that asks the user to enter their name and submit it.
  • The hello() function handles the form data and returns a greeting with the user's name.
  1. Using Flask with a database:

This code shows how to use Flask with a SQLite database.

Code:

from flask import Flask, render_template
import sqlite3

app = Flask(__name__)

@app.route('/')
def index():
    conn = sqlite3.connect('mydatabase.db')
    c = conn.cursor()
    c.execute('SELECT * FROM users')
    users = c.fetchall()
    conn.close()
    return render_template('index.html', users=users)

if __name__ == '__main__':
    app.run()

Explanation:

  • The index() function connects to a SQLite database called mydatabase.db, selects all the rows from the users table, and passes them to the index.html template using the render_template() method.
Write 5 simple beginner python coding flask, explain

原文地址: https://www.cveoy.top/t/topic/okz 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录