Python Flask Web Server

Posted on

To start install MariaDB package for development

sudo apt-get install libmariadbclient-dev

Create Directory for site and templates directory

mkdir net
cd net
mkdir templates

Create a virtual environment called myenv

python3 -m venv myenv

Activate the myenv python environment

source myenv/bin/activate

Check version of Python for virtual environment

sudo nano ~/.profile

At the bottom of the file, type in the following line:

VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3.5

Exit the virtual environment

source ~/.profile

Install Flask in the virtual environment

pip3 install flask

Install the SQL python packages:

pip3 install mysql
pip3 install mysql-connector  

Create Database Schema

login to MariaDB as root and create database and tables

mariadb -u root -p
CREATE DATABASE mydb;
USE mydb;
CREATE TABLE posts ( id INTEGER PRIMARY KEY AUTO_INCREMENT, username CHAR(25), content CHAR(150));
INSERT INTO posts ( username, content) VALUES( ‘gilmedel’, ‘First Post’);

Create Server application

Start by creating app.py file
Then create models.py file
python code:






Create index.html in the template directory for Flask

Save all files then run app.py to start the Python3, Flask, MariaDB server

python3 app.py

Navigate to the webpage http://localhost:5000