提问者:小点点

不允许使用方法:请求的URL不允许使用该方法


我正在尝试使用Flask在本地部署我的决策树。 但我正面临上述错误。 这是我的代码

HTML代码:

<!DOCTYPE html>
<html>
<head>
    <title>my flask hello app</title>
    <style>
        *{
            font-size: 30px;

        }
    </style>
</head>
<body>
<input id="age" type="text" placeholder="age"/></br>
<input id="gender" type="text" placeholder="gender"/></br>
<input id="cp" type="text" placeholder="cp"/></br>
<input id="restbp" type="text" placeholder="restbp"/></br>
<input id="chol" type="text" placeholder="chol"/></br>
<input id="fbs" type="text" placeholder="fbs"/></br>
<input id="restecg" type="text" placeholder="restecg"/></br>
<input id="thalach" type="text" placeholder="thalach"/></br>
<input id="exang" type="text" placeholder="exang" /></br>
<input id="oldpeak" type="text"placeholder="oldpeak"/></br>
<input id="slope" type="text"placeholder="slope"/></br>
<input id="ca" type="text" placeholder="ca"/></br>
<input id="thal" type="text" placeholder="thal"/></br>
<button id="name-button">predict</button>
<p id="greeting">greetings</p>

<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
<script >
    $("#name-button").click(function(event){
        let message={
            
age:$("#age").val(),
gender:$("#gender").val(),
cp:$("#cp").val(),
restbp:$("#restbp").val(),
chol:$("#chol").val(),
fbs:$("#fbs").val(),
restecg:$("#restecg").val(),
thalach:$("#thalach").val(),
exang:$("#exang").val(),
oldpeak:$("#oldpeak").val(),
slope:$("#slope").val(),
ca:$("#ca").val()   ,
thal:$("#thal").val()   
            } // message bracket end

                $.post("http://localhost:5000/ahmed",JSON.stringify(message),function(response){
        $("#greeting").text(response.greeting);
        console.log(response);
    });

    });// onclick end
</script>
</body>
</html>

Python代码:

from flask import request
from flask import jsonify
from flask import Flask

app= Flask(__name__)

@app.route('/ahmed', methods=[ 'post'])
def hello():


    message=request.get_json(force=True)
    age=float(message['age'])
    gender=float(message['gender'])
    cp=float(message['cp'])
    restbp=float(message['restbp'])
    chol=float(message['chol'])
    fbs=float(message['fbs'])
    restecg=float(message['restecg'])
    thalach=float(message['thalach'])
    exang=float(message['exang'])
    oldpeak=message['oldpeak']
    slope=float(message['slope'])
    ca=float(message['ca'])
    thal=float(message['thal'])


    import pandas as pd # load and manipulate data and for One-Hot Encoding
    import numpy as np # calculate the mean and standard deviation
    import matplotlib.pyplot as plt # drawing graphs
    from sklearn.tree import DecisionTreeClassifier # a classification tree
    from sklearn import tree
    from sklearn.model_selection import train_test_split # split  data into training and testing sets
    from sklearn.model_selection import cross_val_score # cross validation
    from sklearn.metrics.classification import confusion_matrix # creates a confusion matrix

    df = pd.read_csv('processed.cleveland.data',header=None)
    df.columns = ['age', 
                'sex', 
                'cp', 
                'restbp', 
                'chol', 
                'fbs', 
                'restecg', 
                'thalach', 
                'exang', 
                'oldpeak', 
                'slope', 
                'ca', 
                'thal', 
                'hd']

    df_no_misssing= df.loc[(df['ca']!='?')&(df['thal']!='?')]
    X = df_no_misssing.drop('hd',axis=1).copy()
    y = df_no_misssing['hd'].copy()
    pd.get_dummies(X,columns=['cp']).head()
    X_encoded = pd.get_dummies(X,columns=['cp','restecg','slope','thal'])
    y_not_zero_index=y>0
    y[y_not_zero_index]=1
    X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=42)
    clf_dt = DecisionTreeClassifier(random_state=42)
    clf_dt = clf_dt.fit(X_train, y_train)

    import pandas as pd
    dict={'age':age, 'sex':gender,'cp':cp,'restbp':restbp,'chol':chol,'fbs':fbs,'restecg':restecg,'thalach':thalach,'exang':exang,'oldpeak':oldpeak,'slope':slope,'ca':ca,'thal':thal}
    dataframe = pd.DataFrame.from_dict(dict, orient='index').T
    if clf_dt.predict(dataframe)[0]==0:
        status="no heart disease"
    else:
        status="heart disease"




    response={
    'greeting':'Patient has , '+status + '!'

    }
    return jsonify(response)  
    

虽然两个endpoint都是匹配的,这是艾哈迈德,但仍然面临上述问题。 此外,python和html都使用post请求,但python端显示的是get请求,而html端显示的是命令提示符下的post请求


共1个答案

匿名用户

@app.route('/Ahmed',methods=['post'])如果希望通过get打开页面,则只启用Post方法,然后需要添加get方法

@app.route('/ahmed', methods=[ 'post', 'get'])