ไม่สามารถ redirect ไปยังหน้าที่ต้องการ

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

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

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

ไม่สามารถ redirect ไปยังหน้าที่ต้องการ

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

หลังจาก submit จะให้ส่งข้อมูลไปยัง http://127.0.0.1:8000/submitform แล้ว redirect กลับมายังหน้าเดิมคือ http://127.0.0.1:5000/ พยายาม redirect ผ่าน javascript แต่มันไม่ยอม redirect
code HTML

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

{% 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>
{% if filename %}
<main>
    <div>
        <img src="{{ url_for('display_image', filename=filename) }}">
    </div>
{% endif %}
    <form action="http://127.0.0.1:8000/submitform" method="post" onsubmit="onSubmit()" enctype="multipart/form-data">
        <div class="flex-container">
            <select class="form-select" name="model" id="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="file" 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>
<script>
    function onSubmit() {
        try {
        window.location.replace("http://127.0.0.1:5000/");
        }
        catch(error) {
            console.error(error);
        }
    }
</script>
โค้ด flask ใช้ในการ render_template html

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

from flask import Flask, url_for, render_template, request
import os
from flask.helpers import flash
from werkzeug.utils import redirect, secure_filename
import tensorflow as tf
from tensorflow.keras.preprocessing.image import load_img, img_to_array
from tensorflow import keras
import requests

app = Flask(__name__)

UPLOAD_FOLDER = 'static/uploads/'
app.secret_key = 'random string'
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
app.config['MAX_CONTENT_LENGTH'] = 16 * 1024 * 1024

# there're type of file that allowed types
ALLOWED_EXTENSIONS = set(['jpg', 'jpeg', 'png'])

last_file_path = ''
loaded_model = keras.models.load_model('../model/gender_classification_model224-224.h5')
prediction = ''

# return the tuple of prediction
# the first index's gender
# the second index's probability
def get_classes(data):
    global loaded_model
    prob = loaded_model.predict(data)
    if prob[0][0] < 0.5:
        return 'woman', 1 - prob[0][0]
    else:
        return 'man', prob[0][0]

# this function check the type of file
def allowed_file(filename):
    return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS

# http://127.0.0.1:5000/
@app.route('/')
def index(): # just render the index.html
    url = 'http://127.0.0.1:8000/submitform'
    response = requests.get(url)
    if response.status_code == 200:
        print(response)
        print(type(response))
    return render_template('index.html')


# Flask section
# http://127.0.0.1:8000/predict
# @app.route('/', methods = ['post'])
# def predict_page():
#     url = 'http://127.0.0.1:8000/submitform'
#     response = requests.get(url)
#     print(type(response))
#     print(response)
#     if response.status_code == 200:
#         pass
#     return render_template('index.html')

if __name__ == '__main__':
    app.run(debug=True) # debug mode is on
ไฟล์ FastAPI ในการทำ Web service API

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

from fastapi import FastAPI, Body, Request, File, UploadFile, Form
import uvicorn
from pathlib import Path


api = FastAPI() # Init FastAPI Ap

# Flask section
# http://127.0.0.1:8000/predict
# @flask_app.get('/')
# def predict_page():
#     url = 'http://127.0.0.1:8000/submitform'
#     response = requests.get(url)
#     print(type(response))
#     print(response)
#     if response.status_code == 200:
#         pass
#     return render_template('index.html')

# http://127.0.0.1:8000/predict
# @flask_app.get('/predicted')
# 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)
แก้ไขล่าสุดโดย annie2301 เมื่อ 17/01/2022 2:36 pm, แก้ไขไปแล้ว 1 ครั้ง.
ภาพประจำตัวสมาชิก
mindphp
ผู้ดูแลระบบ MindPHP
ผู้ดูแลระบบ MindPHP
โพสต์: 41117
ลงทะเบียนเมื่อ: 22/09/2008 6:18 pm
ติดต่อ:

Re: ไม่สามารถ redirect ไปยังหน้าที่ต้องการ

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

ต้องการทำให้ redirect เพื่อทำอะไร ครับ
ติดตาม 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: ไม่สามารถ redirect ไปยังหน้าที่ต้องการ

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

เพื่อส่ง request ไปยัง http://127.0.0.1:8000/submitform ค่ะ อยากให้ Flask รับข้อมูลมาประมวลผลต่อ
ภาพประจำตัวสมาชิก
MBMoo
PHP VIP Members
PHP VIP Members
โพสต์: 25341
ลงทะเบียนเมื่อ: 04/06/2020 10:05 am

Re: ไม่สามารถ redirect ไปยังหน้าที่ต้องการ

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

ลองเช็คหรือยังคะว่าเข้า ฟังก์ชั่น onSubmit หรือเปล่า
ภาพประจำตัวสมาชิก
mindphp
ผู้ดูแลระบบ MindPHP
ผู้ดูแลระบบ MindPHP
โพสต์: 41117
ลงทะเบียนเมื่อ: 22/09/2008 6:18 pm
ติดต่อ:

Re: ไม่สามารถ redirect ไปยังหน้าที่ต้องการ

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

มีแปะตัวอย่าง request / response ไว้ที่นี่ครับ ไม่เกี่ยวกับ redirect
viewtopic.php?p=237021#p237021

กระทู้ถ้าคนอื่นอ่าน แล้ว ตอบแล้ว ไม่ควรกลับไปแก้ไข post เดิมนะครับ
นอกจากคนตอบไม่รู้เรื่อง แล้ว ถือว่าเป็นการเสียมารยาทในการถามตอบเพราะ คนตอบเขาตอบไปตามประเด็นที่ตั้งมาตั้งแต่แรก
ถ้าต้องการแก้ไข หรือถามใหม่ควร quote ข้อความมาแก้ไขในโพสต่อไป
ติดตาม 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: ไม่สามารถ redirect ไปยังหน้าที่ต้องการ

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

mindphp เขียน: 17/01/2022 5:42 pm มีแปะตัวอย่าง request / response ไว้ที่นี่ครับ ไม่เกี่ยวกับ redirect
viewtopic.php?p=237021#p237021

กระทู้ถ้าคนอื่นอ่าน แล้ว ตอบแล้ว ไม่ควรกลับไปแก้ไข post เดิมนะครับ
นอกจากคนตอบไม่รู้เรื่อง แล้ว ถือว่าเป็นการเสียมารยาทในการถามตอบเพราะ คนตอบเขาตอบไปตามประเด็นที่ตั้งมาตั้งแต่แรก
ถ้าต้องการแก้ไข หรือถามใหม่ควร quote ข้อความมาแก้ไขในโพสต่อไป
ตอนนี้ไม่สามารถส่ง request ได้ มันขึ้น error code 405 "Method Not Allowed"
C++ & java & Python-1.png
C++ & java & Python-1.png (20.07 KiB) Viewed 1638 times
แก้ไขล่าสุดโดย annie2301 เมื่อ 17/01/2022 8:10 pm, แก้ไขไปแล้ว 1 ครั้ง.
annie2301
PHP Super Member
PHP Super Member
โพสต์: 289
ลงทะเบียนเมื่อ: 01/12/2021 9:44 am

Re: ไม่สามารถ redirect ไปยังหน้าที่ต้องการ

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

bolue เขียน: 17/01/2022 4:58 pm ลองเช็คหรือยังคะว่าเข้า ฟังก์ชั่น onSubmit หรือเปล่า
มันไม่เข้าอ่าค่ะ ถ้าต้องการให้มัน redirect ไปยังหน้า หลังจากที่กด submit form และประมวลผลคำทำนายของ ML แล้วต้องทำยังไงหรอคะ?
annie2301
PHP Super Member
PHP Super Member
โพสต์: 289
ลงทะเบียนเมื่อ: 01/12/2021 9:44 am

Re: ไม่สามารถ redirect ไปยังหน้าที่ต้องการ

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

เดี๋ยวหนูลองทำแบบไม่ใส่ action ใน form ดูนะคะ จะได้ไม่ต้อง redirect ก่อนหน้านี้ใส่ action="http://127.0.0.1:8000/submitform" มันเลยไปหน้าของ FastAPI
ภาพประจำตัวสมาชิก
mindphp
ผู้ดูแลระบบ MindPHP
ผู้ดูแลระบบ MindPHP
โพสต์: 41117
ลงทะเบียนเมื่อ: 22/09/2008 6:18 pm
ติดต่อ:

Re: ไม่สามารถ redirect ไปยังหน้าที่ต้องการ

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

form ---> Post ----> Process ---> output
ส่วนที่เรียก web service คือส่วนของ Process โดยไม่ต้องมีการ redirect
เมื่อรับค่า post เข้ามาในไฟล์ ส่วน Process[1] คือเตรียมข้อมูลเพื่อส่งต่อไปให้ web service รับ input เพื่อ process[2] ต่อแล้ว response กลับมาที่ process[1]
โชว์ output.

ควรตรวจสอบ web service ที่สร้างขึ้นมาก่อน ด้วย
https://www.mindphp.com/%E0%B8%9A%E0%B8 ... stman.html
ตัวอย่าง postman เรียกทดลองส่งค่าเข้า API
viewtopic.php?f=18&t=80493
ติดตาม 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: ไม่สามารถ redirect ไปยังหน้าที่ต้องการ

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

mindphp เขียน: 18/01/2022 9:23 am form ---> Post ----> Process ---> output
ส่วนที่เรียก web service คือส่วนของ Process โดยไม่ต้องมีการ redirect
เมื่อรับค่า post เข้ามาในไฟล์ ส่วน Process[1] คือเตรียมข้อมูลเพื่อส่งต่อไปให้ web service รับ input เพื่อ process[2] ต่อแล้ว response กลับมาที่ process[1]
โชว์ output.

ควรตรวจสอบ web service ที่สร้างขึ้นมาก่อน ด้วย
https://www.mindphp.com/%E0%B8%9A%E0%B8 ... stman.html
ตัวอย่าง postman เรียกทดลองส่งค่าเข้า API
viewtopic.php?f=18&t=80493
ตอนที่ใช้ mahine learning model มาประมวลผลควรจะประมวลผลใน FastAPI(web service) หรอคะ?
ตอบกลับโพส
  • Similar Topics
    ตอบกลับ
    แสดง
    โพสต์ล่าสุด

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

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