Lecture 6
Lecture 6
non-Programmers
Introduction into
Web development with Flask
2020-11-14
Lecture 6: Introduction into
Web development with Flask
Micro framework
The core of flask is very simple, but extendable
Getting started is quick and easy, but the project
can be scaled up to complex applications
Web development: https://www.fullstackpython.com/web-development.html
directory
After that, you need to
venv\Scripts\activate terminal
activate the virtual
environment
Create the class routes.py inside the app package and add the following
code:
routes.py
from app import app
@app.route('/')
@app.route('/index')
Creating the URL
def index(): Define what should happen when using the
return "Hello, World!" URL
.flaskenv
FLASK_APP=main.py
By hitting the URL or typing the URL into your browser you
should see:
terminal
pip install flask-migrate
class Config(object):
SQLALCHEMY_DATABASE_URI = os.environ.get('DATABASE_URL') or \
'sqlite:///' + os.path.join(basedir, 'app.db')
SQLALCHEMY_TRACK_MODIFICATIONS = False
app = Flask(__name__)
app.config.from_object(Config)
db = SQLAlchemy(app)
migrate = Migrate(app, db)
class Customer(db.Model):
id = db.Column(db.Integer, primary_key=True, unique = True)
username = db.Column(db.String(64), index=True, unique=True)
email = db.Column(db.String(120), index=True, unique=True)
password_hash = db.Column(db.String(128))
def __repr__(self):
return '<Customer {}>'.format(self.username)
class Orders(db.Model):
order_id = db.Column(db.Integer, primary_key=True, unique=True)
description = db.Column(db.String(64), index=True)
customer_id = db.Column(db.Integer, db.ForeignKey('customer.id'))
def __repr__(self):
return '<Order {}>'.format(self.description)
class Customer_table(Table):
id = Col('Id')
username = Col('Username')
email = Col('E-Mail')
class Orders_table(Table):
order_id = Col('Order_Id')
description = Col('Description')
customer_id = Col('Customer_Id')
@app.route('/tables')
def create_tables():
# create a table for the customers
customer_db = models.Customer.query.all()
table_customers = tables.Customer_table(customer_db)
table_customers.border = True
# create a table for the orders
orders_db = models.Orders.query.all()
table_orders = tables.Orders_table(orders_db)
table_orders.border = True
return render_template('tables.html', table1=table_customers, table2 = table_orders)