Flask Installation and quick start and PyCharm Setup

Steven Wang
4 min readOct 9, 2020

--

Installation

Create an environment

Create a project folder and a venv folder within:

$ mkdir myproject
$ cd myproject
$ python3 -m venv venv

Activate the environment

Before you work on your project, activate the corresponding environment:

$ . venv/bin/activate

Install Flask

Within the activated environment, use the following command to install Flask:

$ pip3 install Flask

Quickstart

Eager to get started? This page gives a good introduction to Flask. It assumes you already have Flask installed. If you do not, head over to the Installation section.

A Minimal Application

Quickstart

A minimal Flask application looks something like this:

from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello_world():
return 'Hello, World!'

To run the application you can either use the flask command or python’s -m switch with Flask. Before you can do that you need to tell your terminal the application to work with by exporting the FLASK_APP environment variable:

$ export FLASK_APP=hello.py
$ flask run
* Running on http://127.0.0.1:5000/

PyCharm Setup

設定flasl run功能

configurations

開發模式 FLASK_EVN=development,顯示錯誤訊息偵錯

flask路由的運用,由網址決定帶出哪個def

兩個重點 一個是網址傳參數 一個是傳網址參數的限定格式int

url_for反推def所對應的路徑

Http request CreatUpdateRradDestory

Get 讀取

POST 新增(也能更新/刪除)

路由網址用法

GET http://localhost/
GET http://localhost/1
GET http://localhost/1/edit
POST http://localhost/create
PUT/POST http://localhost/1/update
Delete/POST http://localhost/1/delete

輸入

action
input type : text / submit

安裝MySQL資料庫

ORM : pip install pymysql

pymysql add high level ORM :pip install flask_sqlalchemy

下載mysql download community

安裝

~/.zshrc
export PATH=”/usr/local/mysql/bin:$PATH”

測試安裝成功 $mysql -V

Flask連接mysql資料庫

https://www.maxlist.xyz/2019/10/30/flask-sqlalchemy/

from flask_sqlalchemy import SQLAlchemyapp.config.from_object('config')  吃入config設定

資料庫設定在檔案外才安全,terminal新增檔案 touch config

config.py >>

SQLALCHEMY_DATABASE_URI = "mysql+pymysql://root帳號:123456密碼@127.0.0.1:3306/databasename"
SQLALCHEMY_TRACK_MODIFICATIONS = “false”

$ mysql -p root -u

mysql command line

具有階層概念 一層層進入使用

> show databases; 顯示資料庫list
>create databases databasename; 創建資料庫
>use databasename ;使用資料庫
>create table tablename( columnname type);
create table user(id int ,username varchar(80), email varchar(120));
>show tables; 顯示tables
>describe user;顯示table欄位

設定一個類別 class儲存資料 為用戶資料的結構structure(即資料庫的內容)

class user(db.model):
id = db.column(db.Integer,primary_key=True)
username = db.Column(db.String(80),unique=True)
email = db.Column(db.String(120),unique=True)

進入MVC框架

架構
app folder (v)
app-views folder(v)/__init__.py
app-templates folder
app-model folder(v)/__init__.py
router.py
__init__.py

更改configuration 由router.py進入

設定Package

成為python的package,資料夾內要有__init__.py
這樣這些(v)就可以被其他python file引入

設定初始化app/__init__.py

初始化flask 初始化資料庫
config setup

基底templates/users/layout.html

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Flask MVC</title>
</head>
<body>
{% block body %}{% endblock %}
</body>
</html>
{% block body %}{% endblock %}

以外是不變的
以內是每頁網頁變化的地方
可以維持一樣的風格

使用基底

{% extends "users/layout.html" %}
{% block body %}
...
{% endblock %}

example

{% extends "users/layout.html" %}
{% block body %}

<form action="/create" method="POST" >
<label for="username">Username</label>
<input type="text" name="username"></br>
<label for="email">Email</label>
<input type="text" name="email"></br>
<input type="submit" value="Submit"
</form>

{% endblock %}

建立class資料結構儲存用戶資訊

from .. import db
app folder > __init__.py >內有db引入

Class User 要與MySQL一樣資料庫user名稱
是資料的結構

_create是新增的函式
將取得的username email放入資料的結構中User
db.session.add(user)
db.session.commit()

new.html submit到create.html

因此router.py 轉 views.create()

views

--

--

Steven Wang
Steven Wang

No responses yet