Write 5 simple beginner python coding flask, explain
- 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 functionhello_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.
- 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.
- 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 thenamevariable to theindex.htmltemplate using therender_template()method.
- 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.
- 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 calledmydatabase.db, selects all the rows from theuserstable, and passes them to theindex.htmltemplate using therender_template()method.
原文地址: https://www.cveoy.top/t/topic/okz 著作权归作者所有。请勿转载和采集!