จะทำให้ flask รับค่าจาก fastapi ได้ยังไง

แนะนำ สอบถาม ภาษา C สำหรับผู้เริ่มต้น ภาษา Java ภาษา Python

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

annie2301
PHP Super Member
PHP Super Member
โพสต์: 289
ลงทะเบียนเมื่อ: 01/12/2021 9:44 am

จะทำให้ flask รับค่าจาก fastapi ได้ยังไง

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

ตอนนี้รับค่าจาก form ใน html ได้แล้ว แต่กำลังติดตรงจะให้ flask รับค่าจาก fastapi ได้ยังไง
โค้ด python ที่ใช้เขียน flask และ fastapi

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

from fastapi import FastAPI, Body, Request, File, UploadFile, Form
from fastapi.middleware.wsgi import WSGIMiddleware
import uvicorn
from fastapi.staticfiles import StaticFiles
from pathlib import Path
import requests
from flask import Flask, render_template

api = FastAPI() # Init FastAPI App
flask_app = Flask(__name__) # Init Flask App
# Mount Flask on FastAPI
api.mount('/predict', WSGIMiddleware(flask_app))

# Mount static folder on FastAPI
BASE_PATH = Path(__file__).resolve().parent
api.mount(
    "/static",
    StaticFiles(directory=str(BASE_PATH / "static")),
    name="static",
)

# Flask section
# http://127.0.0.1:8000/predict
@flask_app.get('/')
def predict_page():
    return render_template('index.html')
# http://127.0.0.1:8000/predict

# def predict_page_result():
#     url = 'http://127.0.0.1:8000/submitform'
#     response = requests.get(url)
#     print(response)

# FastAPI section
# http://127.0.0.1:8000/submitform
@api.post('/submitform')
async def handle_form(model: str = Form(...), file: UploadFile = File(...)):
    return {'model': model, 'file': file}


if __name__ == '__main__':
    uvicorn.run(api, host='127.0.0.1', port=8000, debug=True)
โค้ดใน HTML หน้าที่สร้าง form

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

{% extends "layout.html" %}

{% block title %}
<title>Test ML</title>
{% endblock %}

{% block content %}
<div class="parent">
    <header>
        <h1>Predict an image</h1>
    </header>
<p>
    <!-- {% with messages = get_flashed_messages() %}
        {% if messages %}
            <ul>
            {% for message in messages %}
                <li>{{ message }}</li>
            {% endfor %}
            </ul>
        {% endif %}
    {% endwith %} -->
</p>
<main>
    {% if filename %}
    <div>
        <!-- <img src="{{ url_for('display_image', filename=filename) }}"> -->
    </div>
    {% endif %}
    <form action="/submitform" method="post" enctype="multipart/form-data">
        <div class="flex-container">
            <select class="form-select" name="model" aria-label="select a model">
                <option value="1" selected>1st model 224*224</option>
                <option value="2">2nd model 90*96</option>
            </select>
        </div>
        <div class="flex-container">
            <input type="file" class="form-control" id="inputGroupFile02" name="file" required>
            <button type="submit" class="btn btn-primary mb-3">Predict</button>
        </div>
    </form>
    <hr>
    <div>
        {% if gender_prediction %}
        <h3>This's a picture of a {{gender_prediction}}.</h3>
        <h4>It was processed by {{model}}</h4>
        {% else %}
        <h3>Nothing's here</h3>
        {% endif %}
    </div>
</div>
</main>
{% endblock %}
ภาพประจำตัวสมาชิก
mindphp
ผู้ดูแลระบบ MindPHP
ผู้ดูแลระบบ MindPHP
โพสต์: 41125
ลงทะเบียนเมื่อ: 22/09/2008 6:18 pm
ติดต่อ:

Re: จะทำให้ flask รับค่าจาก fastapi ได้ยังไง

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

ก่อนรับค่า ได้เราต้อง requests เข้าไปเรียกใช้บริการให้ web service ทำงานให้เรา
แล้ว response มาใช้งานต่อ

ตัวอย่างเรียกใช้ web service จาก python requests/response

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

import requests
token = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZGVudGl0eSI6MSwiaWF0IjoxNTkzNzUwNjE3LCJuYmYiOjE1OTM3NTA2MTcsImV4cCI6MTU5Mzc1MDkxN30.wP1xZtDBjIQ8k2tknsIeNjCOaNEn-wT9rbh6Jo7dZu4'
access_token = 'JWT %s'% token
header = {'Authorization': access_token}

webservice = 'http://127.0.0.1:5012/PDFconvert/1/'
url_convert = 'https://www.google.co.th'

url = webservice+url_convert
response = requests.get(url, headers=header)

with open('htmltopdf_token.pdf', 'wb') as f:
    f.write(response.content)
print response
อีกตัวอย่าง authorize แบบ username, password

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

import requests
files = {'fileupload': open('[ path ไฟล์ HTML ]', 'rb')}
payload = {'typefile': '[ ประเภทที่จะแปลง คือ 1 กับ 2 ]'}
r=requests.post("'http://[ hostname : port ]/upload",files=files,data=payload, auth=('username','password'))
ติดตาม VDO: http://www.youtube.com/c/MindphpVideoman
ติดตาม FB: https://www.facebook.com/pages/MindphpC ... 9517401606
หมวดแชร์ความรู้: https://www.mindphp.com/forums/viewforum.php?f=29
รับอบรม และพัฒนาระบบ: https://www.mindphp.com/forums/viewtopic.php?f=6&t=2042
annie2301
PHP Super Member
PHP Super Member
โพสต์: 289
ลงทะเบียนเมื่อ: 01/12/2021 9:44 am

Re: จะทำให้ flask รับค่าจาก fastapi ได้ยังไง

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

mindphp เขียน: 14/01/2022 5:05 pm ก่อนรับค่า ได้เราต้อง requests เข้าไปเรียกใช้บริการให้ web service ทำงานให้เรา
แล้ว response มาใช้งานต่อ

ตัวอย่างเรียกใช้ web service จาก python requests/response

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

import requests
token = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZGVudGl0eSI6MSwiaWF0IjoxNTkzNzUwNjE3LCJuYmYiOjE1OTM3NTA2MTcsImV4cCI6MTU5Mzc1MDkxN30.wP1xZtDBjIQ8k2tknsIeNjCOaNEn-wT9rbh6Jo7dZu4'
access_token = 'JWT %s'% token
header = {'Authorization': access_token}

webservice = 'http://127.0.0.1:5012/PDFconvert/1/'
url_convert = 'https://www.google.co.th'

url = webservice+url_convert
response = requests.get(url, headers=header)

with open('htmltopdf_token.pdf', 'wb') as f:
    f.write(response.content)
print response
อีกตัวอย่าง authorize แบบ username, password

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

import requests
files = {'fileupload': open('[ path ไฟล์ HTML ]', 'rb')}
payload = {'typefile': '[ ประเภทที่จะแปลง คือ 1 กับ 2 ]'}
r=requests.post("'http://[ hostname : port ]/upload",files=files,data=payload, auth=('username','password'))
ถ้าต้องการให้ flask รับ response จาก FastAPI โดยอัตโนมัติ จะต้องเขียนยังไงคะ
ภาพประจำตัวสมาชิก
mindphp
ผู้ดูแลระบบ MindPHP
ผู้ดูแลระบบ MindPHP
โพสต์: 41125
ลงทะเบียนเมื่อ: 22/09/2008 6:18 pm
ติดต่อ:

Re: จะทำให้ flask รับค่าจาก fastapi ได้ยังไง

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

ดูตัวอย่างโค้ดแรกครับ ไม่เข้าใจส่วนไหน ยกมาเป็นส่วนๆ
ส่วนของ response มีอยู่ในนี้
viewtopic.php?p=237021#p237021
ติดตาม VDO: http://www.youtube.com/c/MindphpVideoman
ติดตาม FB: https://www.facebook.com/pages/MindphpC ... 9517401606
หมวดแชร์ความรู้: https://www.mindphp.com/forums/viewforum.php?f=29
รับอบรม และพัฒนาระบบ: https://www.mindphp.com/forums/viewtopic.php?f=6&t=2042
annie2301
PHP Super Member
PHP Super Member
โพสต์: 289
ลงทะเบียนเมื่อ: 01/12/2021 9:44 am

Re: จะทำให้ flask รับค่าจาก fastapi ได้ยังไง

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

ตอนนี้ส่ง request ไปยัง FastAPI ไม่ได้ค่ะ viewtopic.php?f=16&t=80911
ภาพประจำตัวสมาชิก
mindphp
ผู้ดูแลระบบ MindPHP
ผู้ดูแลระบบ MindPHP
โพสต์: 41125
ลงทะเบียนเมื่อ: 22/09/2008 6:18 pm
ติดต่อ:

Re: จะทำให้ flask รับค่าจาก fastapi ได้ยังไง

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

ดูตัวอย่างโค้ดแรกครับ ไม่เข้าใจส่วนไหน ยกมาเป็นส่วนๆ
อ่านโค้ดที่ยกมาให้เข้าใจก่อน ครับ

ตัวอย่างโค้ด ตามตัวข้างบน

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

response = requests.get(url, headers=header)
รับ response จากการส่ง requests เข้าไปที่ web service
ติดตาม VDO: http://www.youtube.com/c/MindphpVideoman
ติดตาม FB: https://www.facebook.com/pages/MindphpC ... 9517401606
หมวดแชร์ความรู้: https://www.mindphp.com/forums/viewforum.php?f=29
รับอบรม และพัฒนาระบบ: https://www.mindphp.com/forums/viewtopic.php?f=6&t=2042
annie2301
PHP Super Member
PHP Super Member
โพสต์: 289
ลงทะเบียนเมื่อ: 01/12/2021 9:44 am

Re: จะทำให้ flask รับค่าจาก fastapi ได้ยังไง

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

ตอนี้แก้ปัญหาการส่ง request ได้แล้วค่ะ ต้องสร้าง url สำหรับ get method ถึงจะส่ง request ไปได้
annie2301
PHP Super Member
PHP Super Member
โพสต์: 289
ลงทะเบียนเมื่อ: 01/12/2021 9:44 am

Re: จะทำให้ flask รับค่าจาก fastapi ได้ยังไง

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

ส่วนโค้ดที่สำหรับให้ใช้ get method ให้ส่ง request มาที่ url นี้ได้ ใช้ FastAPI Framework เขียน

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

# http://127.0.0.1:8000/showdata
@api.get('/showdata')
async def get_data():
    global data
    return data
annie2301
PHP Super Member
PHP Super Member
โพสต์: 289
ลงทะเบียนเมื่อ: 01/12/2021 9:44 am

Re: จะทำให้ flask รับค่าจาก fastapi ได้ยังไง

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

annie2301 เขียน: 14/01/2022 6:42 pm
mindphp เขียน: 14/01/2022 5:05 pm ก่อนรับค่า ได้เราต้อง requests เข้าไปเรียกใช้บริการให้ web service ทำงานให้เรา
แล้ว response มาใช้งานต่อ

ตัวอย่างเรียกใช้ web service จาก python requests/response

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

import requests
token = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZGVudGl0eSI6MSwiaWF0IjoxNTkzNzUwNjE3LCJuYmYiOjE1OTM3NTA2MTcsImV4cCI6MTU5Mzc1MDkxN30.wP1xZtDBjIQ8k2tknsIeNjCOaNEn-wT9rbh6Jo7dZu4'
access_token = 'JWT %s'% token
header = {'Authorization': access_token}

webservice = 'http://127.0.0.1:5012/PDFconvert/1/'
url_convert = 'https://www.google.co.th'

url = webservice+url_convert
response = requests.get(url, headers=header)

with open('htmltopdf_token.pdf', 'wb') as f:
    f.write(response.content)
print response
อีกตัวอย่าง authorize แบบ username, password

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

import requests
files = {'fileupload': open('[ path ไฟล์ HTML ]', 'rb')}
payload = {'typefile': '[ ประเภทที่จะแปลง คือ 1 กับ 2 ]'}
r=requests.post("'http://[ hostname : port ]/upload",files=files,data=payload, auth=('username','password'))
ถ้าต้องการให้ flask รับ response จาก FastAPI โดยอัตโนมัติ จะต้องเขียนยังไงคะ
โค้ดส่วนนี้เอาไว้ทำอะไรคะ?

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

with open('htmltopdf_token.pdf', 'wb') as f:
    f.write(response.content)
print response
บุคคลทั่วไป

Re: จะทำให้ flask รับค่าจาก fastapi ได้ยังไง

โพสต์ที่ยังไม่ได้อ่าน โดย บุคคลทั่วไป »

ทำไมใช้เฟรมเวิร์กถึงสองตัว
ตอบกลับโพส
  • Similar Topics
    ตอบกลับ
    แสดง
    โพสต์ล่าสุด

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

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