การใช้ flask framework ในภาษา python

แชร์ความรู้ภาษา Python ไพทอน การเขียนโปรแกรมภาษาไพทอน

Moderator: mindphp, ผู้ดูแลกระดาน

ภาพประจำตัวสมาชิก
jirawoot
PHP VIP Members
PHP VIP Members
โพสต์: 3129
ลงทะเบียนเมื่อ: 17/06/2019 10:30 am

การใช้ flask framework ในภาษา python

โพสต์ที่ยังไม่ได้อ่าน โดย jirawoot »

การใช้ flask framework ในภาษา python
ขั้นตอนแรกใน สร้างโปรเจ็กขึ้นมาใหม่หนึ่งโปรเจ็กตั้งชื่ออะไรได้
Selection_004.png
สร้างโฟรเดอร์ไว้ในโปรเจ็ก คือ templates, static และ ไฟล์ python
Menu_007.png
Menu_007.png (77.61 KiB) Viewed 2008 times
ดังนั้นจะได้
Selection_008.png
Selection_008.png (13.12 KiB) Viewed 2008 times
แล้วให้ทำการติดตั้ง Flask โดยการ pip install flask ใน terminal ของ pycharm
Selection_009.png
Selection_009.png (12.39 KiB) Viewed 2008 times
ถ้าติดตั้งได้ถูกต้องจะขึ้นดังนี้
Successfully installed Jinja2-2.10.1 MarkupSafe-1.1.1 Werkzeug-0.15.4 click-7.0 flask-1.0.3 itsdangerous-1.1.0
Selection_010.png
Selection_010.png (42.3 KiB) Viewed 2008 times
ในโฟรเดอร์ templates ให้สร้างไฟล์ HTML ขึ้นมาก ส่วน static จะเป็นไฟล์ CSS, JS อยู่ภายในไฟล์
Selection_011.png
Selection_011.png (9.74 KiB) Viewed 2008 times
ในไฟล์ index.py ให้ทำการเรียกใช้ flask โดยใช้คำสั่ง

โค้ด: เลือกทั้งหมด

from flask import Flask, render_template
app=Flask(__name__)

@app.route('/')
def index():
    return render_template("home.html")

if __name__ == '__main__':
   app.run()
จะเป็นโค้ดหน้า home.html

โค้ด: เลือกทั้งหมด

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<p>HELLO PYTHON</p>
</body>
</html>
เสร็จแล้วรันจะได้ IP มาให้คลิก หรือ copy ไปเปิดในbrowser
Selection_0122.png
Selection_0122.png (26.68 KiB) Viewed 2008 times
ก็จะได้ดังรูปภาพ
Title - Google Chrome_013.png
อ้างอิง
https://www.mindphp.com/บทเรียนออนไลน์/python-framework-flask/6442-flask-framework-html-css-python.html
  • Similar Topics
    ตอบกลับ
    แสดง
    โพสต์ล่าสุด

ผู้ใช้งานขณะนี้

สมาชิกกำลังดูบอร์ดนี้: ไม่มีสมาชิกใหม่ และบุคลทั่วไป 86