Python flask : TypeError: 'NoneType' object is not subscriptable [duplicate]

Clash Royale CLAN TAG#URR8PPP
Python flask : TypeError: 'NoneType' object is not subscriptable [duplicate]
This question already has an answer here:
I am using flask to deploy my chatbot deep learning model. The model will run well when I run locally on python console. However when I try to deploy with flask, I get this exception:
Traceback (most recent call last): File
"C:UserstabAppDataLocalProgramsPythonPython35libsite-packagesflaskapp.py",
line 2309, in call
return self.wsgi_app(environ, start_response) File "C:UserstabAppDataLocalProgramsPythonPython35libsite-packagesflaskapp.py",
line 2295, in wsgi_app
response = self.handle_exception(e) File "C:UserstabAppDataLocalProgramsPythonPython35libsite-packagesflaskapp.py",
line 1741, in handle_exception
reraise(exc_type, exc_value, tb) File "C:UserstabAppDataLocalProgramsPythonPython35libsite-packagesflask_compat.py",
line 35, in reraise
raise value File "C:UserstabAppDataLocalProgramsPythonPython35libsite-packagesflaskapp.py",
line 2292, in wsgi_app
response = self.full_dispatch_request() File "C:UserstabAppDataLocalProgramsPythonPython35libsite-packagesflaskapp.py",
line 1815, in full_dispatch_request
rv = self.handle_user_exception(e) File "C:UserstabAppDataLocalProgramsPythonPython35libsite-packagesflaskapp.py",
line 1718, in handle_user_exception
reraise(exc_type, exc_value, tb) File "C:UserstabAppDataLocalProgramsPythonPython35libsite-packagesflask_compat.py",
line 35, in reraise
raise value File "C:UserstabAppDataLocalProgramsPythonPython35libsite-packagesflaskapp.py",
line 1813, in full_dispatch_request
rv = self.dispatch_request() File "C:UserstabAppDataLocalProgramsPythonPython35libsite-packagesflaskapp.py",
line 1799, in dispatch_request
return self.view_functionsrule.endpoint File "C:Chatbot-Flask-Server-masterChatbot-Flask-Server-masterapp-.py",
line 60, in prediction
response = pred(str(request.json['message'])) TypeError: 'NoneType' object is not subscriptable
Here is my code:
# webapp
app = Flask(__name__, template_folder='./')
@app.route('/prediction', methods=['POST', 'GET'])
def prediction():
response = pred(str(request.json['message']))
return jsonify(response)
@app.route('/')
def main():
return render_template('index.html')
if __name__ == '__main__':
app.run(debug=True)
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
request.json
None
request.json['message']
if request.json:
You're probably dealing with this issue. Use
request.get_json() instead, or be specific in the request.– Abdou
Aug 12 at 16:21
request.get_json()
@Abdou
request.get_json() give TypeError: 'method' object is not subscriptable– ganz
Aug 12 at 17:43
request.get_json()
TypeError: 'method' object is not subscriptable
@MartijnPieters I have change it but the exception has changet to
TypeError: The view function did not return a valid response. The function either returned None or ended without a return statement.– ganz
Aug 12 at 17:45
TypeError: The view function did not return a valid response. The function either returned None or ended without a return statement.
1 Answer
1
Give this a try an see if that print statement prints anything.
import json
app = Flask(__name__, template_folder='./')
@app.route('/prediction', methods=['POST', 'GET'])
def prediction():
text = str(request.data)
print(request.data) #To check what you are sending
text = json.loads(text)
response = pred(str(text['message']))
return jsonify(response)
@app.route('/')
def main():
return render_template('index.html')
if __name__ == '__main__':
app.run(debug=True)
Stil not work, I got this
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0) ,and it doesn't print anything– ganz
Aug 13 at 13:32
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
If the
print(request.data) isnt printing anything then you are not sending a valid request, So the JSON is throwing keyerror, Send a valid request then try or else tell us how you are sending the POST or GET request– Yash Kumar Atri
Aug 14 at 6:58
print(request.data)
This is how I send the POST
<form method=POST action=" url_for('prediction') "> <input type=text name="Enter your message" value="" maxlength="100"> <input type="submit" value="Submit" > </form>– ganz
Aug 14 at 9:04
<form method=POST action=" url_for('prediction') "> <input type=text name="Enter your message" value="" maxlength="100"> <input type="submit" value="Submit" > </form>
Give this style a try first, Change the url and port number accordingly
<form action = "http://localhost:5505/prediction" method = "POST"> <p>Text <input type = "text" name = "Name" style="height:200px" /></p> <p><input type = "submit" value = "submit" /></p></form>– Yash Kumar Atri
Aug 14 at 10:32
<form action = "http://localhost:5505/prediction" method = "POST"> <p>Text <input type = "text" name = "Name" style="height:200px" /></p> <p><input type = "submit" value = "submit" /></p></form>
Thanks, but
Method Not Allowed The method is not allowed for the requested URL.– ganz
Aug 14 at 16:55
Method Not Allowed The method is not allowed for the requested URL.
request.jsonisNone, sorequest.json['message']fails. You want to test first:if request.json:to ensure it's not an empty value!– Martijn Pieters♦
Aug 12 at 16:21