Flask framework basics.
1. Install new virtual enviroment $ python3 -m venv vflask then start it $ source vflask/bin/activate
2. Install flask $ pip install flask
3. Simplest app:
from flask import Flask
application = Flask(__name__)
@application.route('/') # route
def func():
return 'Pythongeeks'
if __name__=='__main__':
app.debug=True
app.run() # Start flask server
3.1. It uses port 5000 http://localhost:5000/ to change port app.run(host='localhost', port=5001, debug=True)
3.2 To add another route:
@app.route('/') # route 1
def walter():
return 'base route'
@app.route('/<vehicle>') # route 2
def fun(vehicle):
return f'Programming, {vehicle}'
or django analog:
def elephant(phone):
return f'Hello!'
app.add_url_rule('/user/<phone>', 'elephant', elephant)
4. Run the script with python $ python3 script.py starts development server. To make server run even if presented an error add --lazy-loading
4.1 Or you could avoid use app.run() inside of script and run $ flask --app myapplication run
5. Put templates in the folder named 'template' then use:
from flask import render_template
return render_template('hell.html', nomme=nomme)
5.1. Connect js file:
<script type = "text/javascript" src = "{{ url_for('static', filename = 'hell.js') }}" ></script>
6. To install $ pip install flask-WTF
6.1. cannot import name 'TextField' from 'wtforms'
6.2. Use from wtforms import StringField instead
6.3. To install SQLalchemy: $ pip install flask-sqlalchemy
6.3.1. How to migrate tables in the database? $ pip install Flask-Migrate // Add in app.py:
from flask_migrate import Migrate, migrate # Import for Migrations
migrate = Migrate(app, db) # Settings for migration
6.3.2. Then type in python console: flask db init -> flask db migrate -m "Initialize databas migration"
6.3.3. How to check SQLite from command line? $ sqlite3
6.3.4. To create database go to directory you need then $ sqlite3 nameofdatabase.db
6.3.5. To create table: create table tablename(text, priority INTEGER);
6.3.6. To insert row: insert into tablename values('Text about discussions', 911);
6.3.7. To get rows: select * from tablename;
6.3.8. To create table:
CREATE TABLE mind (
lion varchar(30) primary key,
mouse text,
biden real,
winter INT );
6.4. To set value in SQlite use: UPDATE car SET movie=2 WHERE director='Diego';
7. Install $ pip install flask-mysqldb
7.1. import:
from flask_mysqldb import MySQL
import MySQLdb.cursors
7.2. Set connection data:
app.config['MYSQL_HOST'] = 'localhost'
app.config['MYSQL_USER'] = 'user'
app.config['MYSQL_PASSWORD'] = '12345'
app.config['MYSQL_DB'] = 'titanik'
mysql = MySQL(app)
7.3. How to write into database:
cursor = mysql.connection.cursor(MySQLdb.cursors.DictCursor)
cursor.execute('INSERT INTO wind VALUES (NULL, % s)', (sign, ))
mysql.connection.commit()
7.4. How to access database:
cursor = mysql.connection.cursor(MySQLdb.cursors.DictCursor)
cursor.execute('SELECT * FROM wind WHERE livestock = % s', (chicken, ))
truck = cursor.fetchone()
6.3.9. To add 404 custom error page:
@app.errorhandler(404)
def not_found(e):
return render_template("404.html")