Home > Mobile >  Flask: How to pass the value of a variable from app.py and display it on a html page
Flask: How to pass the value of a variable from app.py and display it on a html page

Time:01-07

I need help please, so this function in my app.py queries an API and gets a response that comprises an array called "choices" and the first object of the array called "text" is what I am retrieving and saving in the "answer" variable

`

def openAIQuery (query):
    response = openai.Completion.create(
        model="text-davinci-003",
        prompt= query,
        temperature=0.7,
        max_tokens=1000,
        top_p=1,
        frequency_penalty=0,
        presence_penalty=0)

    if 'choices' in response:
        if len(response['choices']) > 0:
            answer = response["choices"][0]["text"]
        else:
            answer = "Oops sorry, you beat DaVinci this time"
    else:
        answer = "Oops sorry you beat DaVinci this time"

    return answer

This function is another function in my app.py that should assign the returned "answer" in the openAIAnswer variable

@app.route('/product-description', methods=["GET", "POST"])
def productDescription():

    if request.method == 'POST':
        submission = request.form['productDescription']
        query = "Write a detailed product description for: {}".format(submission),
        openAIAnswer = openAIQuery(query)

        prompt = 'DaVinci Suggestions for {} are: '.format( submission )

    return render_template("product-description.html" )

`

my problem is that i wanted to display the value of the prompt and openAIAnswer in another html page in my template directory but it does work

`

 <div >
    <div >
      <div >
        <h3 >{{prompt}}</h3>
        <p >{{openAIAnswer}}</p>

      </div>
    </div>
  </div>

` please i need solution

whenever i run the app and perform the necessary actions, I see from the error logs that my API request returned with

response_code=200

but my HTML page does not display the openAIAnswer and also the prompt on the html page which exactly what I want to achieve

CodePudding user response:

I later used The locals() method that returns a dictionary with all the local variables and symbols for the current program for example, i added it here return render_template("product-description.html", **locals( )) and the code worked

  • Related