簡體   English   中英

從python打印輸出到網頁並從網頁接收輸入並處理到python

[英]printing output to webpage from python and receive input from webpage and process to python

這是python文件

@app.route('/processor', methods=["GET", "POST"])
def chatbot_response():
    msg = request.form['question']
    ints = predict_class(msg, model)
    if ints == int(0):
        ints = str(ints)
        ints = list(map(int,ints))
        ints.clear()

    if (len(ints) == 0):
        ints = [{"intent": "no_other_symptoms", "probability": "1.0"}]
        sorry = {'Sorry, I cant\'t understand what you mean.', 'Sorry! Can you type another word?'}

    if request.method == 'POST':
        prediction = getPrediction(ints)
        list_of_intents = intents['intents']
        result = ints[-1]['intent']
        if (result == 'no_other_symptoms'):
            result = "Hi, sorry to say that I can't find related diseases in your case."
            print(result)
        else:
            for i in list_of_intents:
                if (i['tag'] == result):
                    if (len(symptoms_list) == 0):
                        result = random.choice(i['responses'])
                        # print(result)
                    elif (len(symptoms_list) < 2 and result != 'no_other_symptoms'):
                        result = random.choice(i['responses'])
                        print(result)
                        inp_moresym = input().lower().strip()
                        if 'no' in inp_moresym:
                            result = "I have diagnosed your symptoms and I guess you are having "
                            result += prediction['detected_disease_probabilities'][0]['disease'] + " . "
                        else:
                            abc = predict_class(inp_moresym, model)
                            # tmp = []
                            for result in abc:
                                ints.append(result)
                            prediction = getPrediction(ints)
                            result = "I have diagnosed your symptoms and I guess you are having "
                            result += prediction['detected_disease_probabilities'][0]['disease'] + " . "
                    elif (len(symptoms_list) >= 2):
                        index_highest_disease_probability = prediction['detected_disease_probabilities'][0]['index']
                        for rule in rules[index_highest_disease_probability]:
                            if (int(rule) not in detected_rules):
                                result = "Hi, based on my dataset, people that have your symptoms are also have " + \
                                         symptoms[int(rule) - 1] + " , do you also feel it? symtoms no: " + rule
                                print(result)
                                user_input = input().lower().strip()
                                if 'yes' in user_input:
                                    ints.append({'intent': symptoms[int(rule) - 1], 'probability': 1})
                                    prediction = getPrediction(ints)
                                    result = "I have diagnosed your symptoms and I guess you are having "
                                    result += prediction['detected_disease_probabilities'][0]['disease'] + " . "
                                if 'no' in user_input:
                                    result = "I have diagnosed your symptoms and I guess you are having "
                                    result += prediction['detected_disease_probabilities'][0]['disease'] + " . "

    return jsonify({"response": result })

我的問題是如何將結果打印到網頁而不是在 python 內部,然后讓用戶再次輸入網頁的輸入並帶回 python?

elif (len(symptoms_list) < 2 and result != 'no_other_symptoms'):
      result = random.choice(i['responses'])
      print(result)
      inp_moresym = input().lower().strip()

這是 index.html

 <div class="row"> <div class="col-lg-9"> <form action = "/processor" method = "POST"> <p><input class="form-control" type = 'text' name = 'question' id ="question"/></p> </form> </div> <div class="col-lg-3"> <button class="btn btn-primary btn-block" id="submit-button">Send</button> </div> </div> <br> <div class="row"> <div class="col"> <p id="response"></p> </div> </div> </div> <script src="/static/jquery.min.js" ></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script> <script> jQuery(document).ready(function() { $("#submit-button").click(function(e) { // #for id ,$ for access jQuery e.preventDefault(); $.ajax({ type: "POST", url: "/processor", //connect to flask data: { question: $("#question").val() }, success: function(result) { $("#response").append("<br>Me: "+$("#question").val()+ "<br> Karabo: "+result.response); $("#question").val("") }, error: function(result) { alert('error'); } });

輸入消息的示例圖像

症狀列表少於 2,它將需要用戶的輸入,但它在 python 中而不是在 html 中

這是聊天機器人在醫療保健中的程序,首先用戶輸入症狀,當症狀列表少於兩個時,系統會告訴用戶輸入更多症狀。

如果您希望將該值打印在網頁上,則必須將該值返回給 js 並使用 DOM 操作在網頁上顯示。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM