0% found this document useful (0 votes)
3 views5 pages

Python Notes Set 8

The document provides an overview of Flask, a web framework for Python, covering basic setup, routing, handling parameters, templates, forms, and database integration using Flask-SQLAlchemy. It includes code snippets demonstrating how to create routes, handle user input, and render templates. Additionally, it explains how to configure a database connection and define a model for data storage.

Uploaded by

fake786king
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views5 pages

Python Notes Set 8

The document provides an overview of Flask, a web framework for Python, covering basic setup, routing, handling parameters, templates, forms, and database integration using Flask-SQLAlchemy. It includes code snippets demonstrating how to create routes, handle user input, and render templates. Additionally, it explains how to configure a database connection and define a model for data storage.

Uploaded by

fake786king
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Page 1: Flask Basics

from flask import Flask

app = Flask(__name__)

@app.route('/')

def home():

return "Hello, Flask!"

if __name__ == '__main__':

app.run(debug=True)
Page 2: Flask Routing and Params

@app.route('/user/<name>')

def user(name):

return f"Hello {name}"

Query Params:

request.args.get('param')

POST Data:

request.form['field']
Page 3: Flask Templates

from flask import render_template

@app.route('/')

def index():

return render_template('index.html')

Pass data to template:

return render_template('index.html', name="Alice")


Page 4: Flask Forms

from flask import request

@app.route('/submit', methods=['POST'])

def submit():

name = request.form['name']

return f"Submitted {name}"


Page 5: Flask with Database

Use Flask-SQLAlchemy:

from flask_sqlalchemy import SQLAlchemy

app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///data.db'

db = SQLAlchemy(app)

Create model:

class User(db.Model):

id = db.Column(db.Integer, primary_key=True)

name = db.Column(db.String(80))

You might also like