0

Hello and thanks in advance for any help. I am trying to set up a ReactJS & Flask Web App, but i am having trouble getting the data into Flask, i am using Insomnia to test send the data to Flask, and the POST request returns code 200, but i keep on getting the error on the printscreen below, UnboundLocalError: local variable 'text' referenced before assignment. The string is not passed to the TTS (text-to-speech) class for processing, when i use direct assignment for the strings on the comented code bellow the imports works fine.

I have tried to send the data with JSON, now i am trying with form format, it returns the same error.

Can you help me, please, and take a look at my code ?

from flask import Flask, render_template, request
import speak

# text = "AI AI minha machadinha !!"
# lang = "pt"

app = Flask(__name__, static_folder="../static/dist", template_folder="../static")
@app.route("/", methods=["GET","POST"])
def index():
    return render_template("index.html")

@app.route("/hello", methods=["GET","POST"])
def hello():
    if request.method == "POST":
        text = request.form["text"]
        lang = request.form["lang"]
        print("passou")
    return speak.get_speak(text,lang)

if __name__ == "__main__":
    app.run()

Insomnia code 200 message

error on console log

kajó
  • 15
  • 1
  • 6

1 Answers1

0

Try this below :

@app.route("/hello", methods=["GET","POST"])
def hello():
    text = ''
    if request.method == "POST":
        text = request.form["text"]
        lang = request.form["lang"]
        print("passou")
        return speak.get_speak(text,lang)

This is beacause your text is defined inside if condition and your return is outside the if condition. You need to define it above the if and give it a default value.

Abhishek Kulkarni
  • 1,747
  • 1
  • 6
  • 8
  • Thank you for getting back to me. I tried and solved that error, but now is passing a empty string into the get_speak function, with some debugging with a print i know it enters the get_speak function but with empty variables. Any idea why does it enter the if condition (if it would not enter the if condition it wouldn't return the get_speak function i believe ?) and does not assign the form values ? I have tried to change it to JSON, change it to GET instead of POST and similar errors occur... – kajó Apr 10 '20 at 19:18
  • I guess there is some issue with the request.form then , you’re not getting the values from the front end properly it seems! – Abhishek Kulkarni Apr 11 '20 at 04:32
  • I just built a very simple front end template with a button to trigger the function, and i am using Insomnia to test send the data to Flask, if you check out the first picture in my original post, you can see the 200 message, seems everything is ok... – kajó Apr 11 '20 at 08:50
  • Try to return it inside the if block. Have edited my code. – Abhishek Kulkarni Apr 11 '20 at 09:51
  • This is what i get now : File "/home/kajo/Desktop/TTS/TextToSpeech/server/venv/lib/python3.7/site-packages/flask/app.py", line 2097, in make_response "The view function did not return a valid response. The" TypeError: The view function did not return a valid response. The function either returned None or ended without a return statement. – kajó Apr 11 '20 at 13:48