Python Is Throwing "syntax Error" While Using @app.route
Python is throwing 'Syntax Error' when I compile the code below. File 'app.py', line 11 @app.route('/') ^ SyntaxError: invalid syntax I'm not sure what it means. from fla
Solution 1:
You must define the function after the route decorator, i.e. after @app.route
Updated code
@app.route("/")deffunction_main():
#all logics herereturn render_template('index.html', pewds_sub = subsp, tseries_sub = subt)
Solution 2:
Make sure to process your calculations inside function else try to pass those argument in defined function.
from flask import Flask, render_template
import urllib.request
import json
import time
app = Flask(__name__ ,template_folder='template')
namep = "PewDiePie"
namet = "TSeries"
key = "MY_API_KEY"@app.route("/")defmain():
for x inrange(5):
time.sleep(2)
datat = urllib.request.urlopen("https://www.googleapis.com/youtube/v3/channels?part=statistics&forUsername="+namep+"&key="+key).read()
datap = urllib.request.urlopen("https://www.googleapis.com/youtube/v3/channels?part=statistics&forUsername="+namet+"&key="+key).read()
subt = json.loads(datat)["items"][0]["statistics"]["subscriberCount"]
subsp = json.loads(datap)["items"][0]["statistics"]["subscriberCount"]
return render_template('index.html', pewds_sub = subsp, tseries_sub = subt)
if __name__ == "__main__":
app.run(debug=True, host="0.0.0.0", port=80)
Solution 3:
in my case,
I initiated a try block just above for the database connection and forget to put catch block, that's why I have encountered this error.
so I suggest anyone facing the same error,
should check the code above @app.route('/')
because if you have import flask
properly this should work pretty fine syntax error in this statement usually indicates that you might have a problem above this line and not at that line.
Post a Comment for "Python Is Throwing "syntax Error" While Using @app.route"