上傳檔案到「Flask」
This commit is contained in:
parent
32f015a6c9
commit
ec357ad0f8
460
Flask/README.md
460
Flask/README.md
|
@ -1,229 +1,233 @@
|
||||||
# Flask
|
# Flask
|
||||||
## 介紹
|
## 介紹
|
||||||
### Flask 是一個使用 Python 語言開發的輕量級 Web 應用框架。近年來因為Python簡單易學的語法以及強大的生態系統,所以在Web開發領域也逐漸受到青睞。其中Flask的哲學是保持核心簡單但擴充性強,它只提供了路由、模板、靜態檔案等最基本的功能,其他大多數功能則是通過擴展提供,這樣可以讓我們按需求選擇。
|
### Flask 是一個使用 Python 語言開發的輕量級 Web 應用框架。近年來因為Python簡單易學的語法以及強大的生態系統,所以在Web開發領域也逐漸受到青睞。其中Flask的哲學是保持核心簡單但擴充性強,它只提供了路由、模板、靜態檔案等最基本的功能,其他大多數功能則是通過擴展提供,這樣可以讓我們按需求選擇。
|
||||||
### Flask強調概念明確、低耦合度和可擴充性。和Django等全能框架不同,
|
### Flask強調概念明確、低耦合度和可擴充性。和Django等全能框架不同
|
||||||
##
|
### Flask 用template 與 static 2個資料夾做區分
|
||||||
## 程式碼說明
|
#### template 用於存放HTML等網頁
|
||||||
### 首先先以Python切入
|
#### static 用於存放靜態文件,包刮JS、CSS、img等
|
||||||
### Python
|
#### 若需要更進階的需求,建議去前端找Bootstrap,那邊以Flask去介紹如何套版,可以快速製作漂亮的網站頁面
|
||||||
```
|
##
|
||||||
# 匯入會用到的庫
|
## 程式碼說明
|
||||||
from flask import Flask
|
### 首先先以Python切入
|
||||||
from flask import render_template
|
### Python
|
||||||
from flask import Flask, request, jsonify
|
```
|
||||||
import threading
|
# 匯入會用到的庫
|
||||||
import requests, socket
|
from flask import Flask
|
||||||
import pymysql
|
from flask import render_template
|
||||||
# from flask_cors import CORS # 若有跨域需求再開,練習通常用不到
|
from flask import Flask, request, jsonify
|
||||||
```
|
import threading
|
||||||
|
import requests, socket
|
||||||
### 接著我這邊與網路上的寫法有些不同,我直接寫成物件導向的方式,實測下來沒太大問體
|
import pymysql
|
||||||
#### 先設定多線呈方式,及設定好資料庫連線位置
|
# from flask_cors import CORS # 若有跨域需求再開,練習通常用不到
|
||||||
```
|
```
|
||||||
class Website(threading.Thread):
|
|
||||||
def __init__(self):
|
### 接著我這邊與網路上的寫法有些不同,我直接寫成物件導向的方式,實測下來沒太大問體
|
||||||
threading.Thread.__init__(self)
|
#### 先設定多線呈方式,及設定好資料庫連線位置
|
||||||
self.db = pymysql.connect(host='140.125.21.65', port=3307, user='VIP125', passwd='@VIPvip125', db='VIP125', charset='utf8')
|
```
|
||||||
self.app = Flask(__name__)
|
class Website(threading.Thread):
|
||||||
# CORS(self.app)
|
def __init__(self):
|
||||||
|
threading.Thread.__init__(self)
|
||||||
```
|
self.db = pymysql.connect(host='140.125.21.65', port=3307, user='VIP125', passwd='@VIPvip125', db='VIP125', charset='utf8')
|
||||||
### 路由設定
|
self.app = Flask(__name__)
|
||||||
#### 這邊統一教學,我設定成前後端分離的方式,將頁面與API做個區分,之後若是要單獨架設頁面或是API都可以直接修改
|
# CORS(self.app)
|
||||||
```
|
|
||||||
#前端顯示
|
```
|
||||||
self.app.add_url_rule('/', 'Index', self.Index)
|
### 路由設定
|
||||||
self.app.add_url_rule('/next/<id>', 'Next', self.Next)
|
#### 這邊統一教學,我設定成前後端分離的方式,將頁面與API做個區分,之後若是要單獨架設頁面或是API都可以直接修改
|
||||||
|
```
|
||||||
# API
|
#前端顯示
|
||||||
self.app.add_url_rule('/api/get_data', 'get_data', self.get_data,methods=['get'])
|
self.app.add_url_rule('/', 'Index', self.Index)
|
||||||
self.app.add_url_rule('/api/get_one_data/<id>', 'get_one_data', self.get_one_data, methods=['get'])
|
self.app.add_url_rule('/next/<id>', 'Next', self.Next)
|
||||||
```
|
|
||||||
|
# API
|
||||||
|
self.app.add_url_rule('/api/get_data', 'get_data', self.get_data,methods=['get'])
|
||||||
### 前端頁面導向
|
self.app.add_url_rule('/api/get_one_data/<id>', 'get_one_data', self.get_one_data, methods=['get'])
|
||||||
#### 這邊提供純頁面及需要攜帶部分參數的簡單方式
|
```
|
||||||
```
|
|
||||||
# 前端
|
|
||||||
def Index(self):
|
### 前端頁面導向
|
||||||
return render_template('Index.html')
|
#### 這邊提供純頁面及需要攜帶部分參數的簡單方式
|
||||||
def Next(self,id): # 攜帶參數
|
```
|
||||||
return render_template('Next.html',id = id)
|
# 前端
|
||||||
```
|
def Index(self):
|
||||||
### 後端資料
|
return render_template('Index.html')
|
||||||
#### 建議先看過一部份的資料庫語法後再來接觸
|
def Next(self,id): # 攜帶參數
|
||||||
```
|
return render_template('Next.html',id = id)
|
||||||
# 後端API
|
```
|
||||||
def get_data(self):
|
### 後端資料
|
||||||
cursor = self.db.cursor()
|
#### 建議先看過一部份的資料庫語法後再來接觸
|
||||||
sql = "select * from teaching_sql_1"
|
```
|
||||||
cursor.execute(sql)
|
# 後端API
|
||||||
data = cursor.fetchall()
|
def get_data(self):
|
||||||
print(f'ALL_data = {data}')
|
cursor = self.db.cursor()
|
||||||
return jsonify(data)
|
sql = "select * from teaching_sql_1"
|
||||||
|
cursor.execute(sql)
|
||||||
def get_one_data(self,id):
|
data = cursor.fetchall()
|
||||||
cursor = self.db.cursor()
|
print(f'ALL_data = {data}')
|
||||||
sql = f"SELECT * FROM teaching_sql_1 WHERE student_id='{id}';"
|
return jsonify(data)
|
||||||
print(sql)
|
|
||||||
cursor.execute(sql)
|
def get_one_data(self,id):
|
||||||
data = cursor.fetchall()
|
cursor = self.db.cursor()
|
||||||
print(f'user_id = {data}')
|
sql = f"SELECT * FROM teaching_sql_1 WHERE student_id='{id}';"
|
||||||
return jsonify(data)
|
print(sql)
|
||||||
|
cursor.execute(sql)
|
||||||
```
|
data = cursor.fetchall()
|
||||||
### 完整程式
|
print(f'user_id = {data}')
|
||||||
```
|
return jsonify(data)
|
||||||
from flask import Flask
|
|
||||||
from flask import render_template
|
```
|
||||||
from flask import Flask, request, jsonify
|
### 完整程式
|
||||||
import threading
|
```
|
||||||
import requests, socket
|
from flask import Flask
|
||||||
import pymysql
|
from flask import render_template
|
||||||
|
from flask import Flask, request, jsonify
|
||||||
# from flask_cors import CORS
|
import threading
|
||||||
|
import requests, socket
|
||||||
class Website(threading.Thread):
|
import pymysql
|
||||||
def __init__(self):
|
|
||||||
threading.Thread.__init__(self)
|
# from flask_cors import CORS
|
||||||
self.db = pymysql.connect(host='140.125.21.65', port=3307, user='VIP125', passwd='@VIPvip125', db='VIP125',
|
|
||||||
charset='utf8')
|
class Website(threading.Thread):
|
||||||
self.app = Flask(__name__)
|
def __init__(self):
|
||||||
# CORS(self.app)
|
threading.Thread.__init__(self)
|
||||||
|
self.db = pymysql.connect(host='140.125.21.65', port=3307, user='VIP125', passwd='@VIPvip125', db='VIP125',
|
||||||
#前端顯示
|
charset='utf8')
|
||||||
self.app.add_url_rule('/', 'Index', self.Index)
|
self.app = Flask(__name__)
|
||||||
self.app.add_url_rule('/next/<id>', 'Next', self.Next)
|
# CORS(self.app)
|
||||||
|
|
||||||
# API
|
#前端顯示
|
||||||
self.app.add_url_rule('/api/get_data', 'get_data', self.get_data,methods=['get'])
|
self.app.add_url_rule('/', 'Index', self.Index)
|
||||||
self.app.add_url_rule('/api/get_one_data/<id>', 'get_one_data', self.get_one_data, methods=['get'])
|
self.app.add_url_rule('/next/<id>', 'Next', self.Next)
|
||||||
|
|
||||||
|
# API
|
||||||
|
self.app.add_url_rule('/api/get_data', 'get_data', self.get_data,methods=['get'])
|
||||||
def run(self):
|
self.app.add_url_rule('/api/get_one_data/<id>', 'get_one_data', self.get_one_data, methods=['get'])
|
||||||
#self.app.run()
|
|
||||||
self.app.run(host="0.0.0.0", port="5500")
|
|
||||||
|
|
||||||
# 前端
|
def run(self):
|
||||||
def Index(self):
|
#self.app.run()
|
||||||
return render_template('Index.html')
|
self.app.run(host="0.0.0.0", port="5500")
|
||||||
def Next(self,id): # 攜帶參數
|
|
||||||
return render_template('Next.html',id = id)
|
# 前端
|
||||||
|
def Index(self):
|
||||||
# 後端API
|
return render_template('Index.html')
|
||||||
def get_data(self):
|
def Next(self,id): # 攜帶參數
|
||||||
cursor = self.db.cursor()
|
return render_template('Next.html',id = id)
|
||||||
sql = "select * from teaching_sql_1"
|
|
||||||
cursor.execute(sql)
|
# 後端API
|
||||||
data = cursor.fetchall()
|
def get_data(self):
|
||||||
print(f'ALL_data = {data}')
|
cursor = self.db.cursor()
|
||||||
# 返回 JSON 响应
|
sql = "select * from teaching_sql_1"
|
||||||
return jsonify(data)
|
cursor.execute(sql)
|
||||||
|
data = cursor.fetchall()
|
||||||
def get_one_data(self,id):
|
print(f'ALL_data = {data}')
|
||||||
cursor = self.db.cursor()
|
# 返回 JSON 响应
|
||||||
sql = f"SELECT * FROM teaching_sql_1 WHERE student_id='{id}';"
|
return jsonify(data)
|
||||||
print(sql)
|
|
||||||
cursor.execute(sql)
|
def get_one_data(self,id):
|
||||||
data = cursor.fetchall()
|
cursor = self.db.cursor()
|
||||||
print(f'user_id = {data}')
|
sql = f"SELECT * FROM teaching_sql_1 WHERE student_id='{id}';"
|
||||||
return jsonify(data)
|
print(sql)
|
||||||
|
cursor.execute(sql)
|
||||||
|
data = cursor.fetchall()
|
||||||
if __name__ == '__main__':
|
print(f'user_id = {data}')
|
||||||
website = Website()
|
return jsonify(data)
|
||||||
website.run()
|
|
||||||
```
|
|
||||||
|
if __name__ == '__main__':
|
||||||
### 前端(HTML)
|
website = Website()
|
||||||
#### 這邊就以基礎的HTML與JS進行
|
website.run()
|
||||||
#### 有簡單的利用AJAX去撈資料、頁面跳轉
|
```
|
||||||
#### Index.html
|
|
||||||
```
|
### 前端(HTML)
|
||||||
<!DOCTYPE html>
|
#### 這邊就以基礎的HTML與JS進行
|
||||||
<html lang="en">
|
#### 有簡單的利用AJAX去撈資料、頁面跳轉
|
||||||
|
#### Index.html
|
||||||
|
```
|
||||||
<head>
|
<!DOCTYPE html>
|
||||||
<title>Flask HTML CSS Example</title>
|
<html lang="en">
|
||||||
<link rel="stylesheet" href="{{ url_for('static', filename='css/style.css') }}">
|
|
||||||
|
|
||||||
<link rel="stylesheet" href="{{ url_for('static', filename='lib/bootstrap/dist/css/bootstrap.min.css') }}"/>
|
<head>
|
||||||
<link rel="stylesheet" href="{{ url_for('static', filename='css/site.css') }}" asp-append-version="true"/>
|
<title>Flask HTML CSS Example</title>
|
||||||
|
<link rel="stylesheet" href="{{ url_for('static', filename='css/style.css') }}">
|
||||||
<script src="{{ url_for('static', filename='js/script.js') }}"></script>
|
|
||||||
|
<link rel="stylesheet" href="{{ url_for('static', filename='lib/bootstrap/dist/css/bootstrap.min.css') }}"/>
|
||||||
<script src="{{ url_for('static', filename='js/site.js') }}"></script>
|
<link rel="stylesheet" href="{{ url_for('static', filename='css/site.css') }}" asp-append-version="true"/>
|
||||||
<script src="{{ url_for('static', filename='lib/bootstrap/dist/js/bootstrap.bundle.min.js') }}"></script>
|
|
||||||
<script src="{{ url_for('static', filename='lib/jquery/dist/jquery.min.js') }}" asp-append-version="true"></script>
|
<script src="{{ url_for('static', filename='js/script.js') }}"></script>
|
||||||
|
|
||||||
|
<script src="{{ url_for('static', filename='js/site.js') }}"></script>
|
||||||
|
<script src="{{ url_for('static', filename='lib/bootstrap/dist/js/bootstrap.bundle.min.js') }}"></script>
|
||||||
</head>
|
<script src="{{ url_for('static', filename='lib/jquery/dist/jquery.min.js') }}" asp-append-version="true"></script>
|
||||||
<body>
|
|
||||||
<h1>Flask</h1>
|
|
||||||
|
|
||||||
<div class="size2">
|
</head>
|
||||||
<div>
|
<body>
|
||||||
Hello World!
|
<h1>Flask</h1>
|
||||||
</div>
|
|
||||||
<input type="text" id="value_id" />
|
<div class="size2">
|
||||||
<button onclick="next_clicked()">下一頁</button>
|
<div>
|
||||||
|
Hello World!
|
||||||
<button onclick="get_data()">獲取資料</button>
|
</div>
|
||||||
|
<input type="text" id="value_id" />
|
||||||
</div>
|
<button onclick="next_clicked()">下一頁</button>
|
||||||
|
|
||||||
|
<button onclick="get_data()">獲取資料</button>
|
||||||
|
|
||||||
|
</div>
|
||||||
|
|
||||||
</body>
|
|
||||||
</html>
|
|
||||||
|
|
||||||
<script>
|
|
||||||
function next_clicked(){
|
</body>
|
||||||
var id = document.getElementById("value_id").value
|
</html>
|
||||||
window.location = "/next/" + id;
|
|
||||||
}
|
<script>
|
||||||
function get_data(){
|
function next_clicked(){
|
||||||
$.ajax({
|
var id = document.getElementById("value_id").value
|
||||||
type: "GET",
|
window.location = "/next/" + id;
|
||||||
url: "/api/get_data",
|
}
|
||||||
data: {},
|
function get_data(){
|
||||||
contentType: "application/json",
|
$.ajax({
|
||||||
success: function (Model) {
|
type: "GET",
|
||||||
console.log(Model)
|
url: "/api/get_data",
|
||||||
//set_data(Model)
|
data: {},
|
||||||
|
contentType: "application/json",
|
||||||
}
|
success: function (Model) {
|
||||||
});
|
console.log(Model)
|
||||||
}
|
//set_data(Model)
|
||||||
</script>
|
|
||||||
```
|
}
|
||||||
#### Next.html
|
});
|
||||||
```
|
}
|
||||||
<!DOCTYPE html>
|
</script>
|
||||||
<html lang="en">
|
```
|
||||||
<head>
|
#### Next.html
|
||||||
<meta charset="UTF-8">
|
```
|
||||||
<title>Title</title>
|
<!DOCTYPE html>
|
||||||
</head>
|
<html lang="en">
|
||||||
<body>
|
<head>
|
||||||
<input id="value_id" value='{{ id }}' />
|
<meta charset="UTF-8">
|
||||||
<br>
|
<title>Title</title>
|
||||||
|
</head>
|
||||||
<button onclick="Index_clicked()">首頁</button>
|
<body>
|
||||||
</body>
|
<input id="value_id" value='{{ id }}' />
|
||||||
|
<br>
|
||||||
|
|
||||||
</html>
|
<button onclick="Index_clicked()">首頁</button>
|
||||||
|
</body>
|
||||||
|
|
||||||
<script>
|
|
||||||
function Index_clicked(){
|
</html>
|
||||||
window.location = "/";
|
|
||||||
}
|
|
||||||
</script>
|
<script>
|
||||||
|
function Index_clicked(){
|
||||||
|
window.location = "/";
|
||||||
|
}
|
||||||
|
</script>
|
||||||
```
|
```
|
Loading…
Reference in New Issue
Block a user