How to run your defined function in FLASK for building API

Issue

I have defined a function which prints the movie recommendation based on the input movie.

app=Flask(__name__)
@app.route("/")
def recommend(movie_title):
    print("for movie({})".format(movie_title))
    print("Top 10 movies recommended are")
    movie_title_rating=final_movie_table["{}".format(movie_title)]
    related_movies=final_movie_table.corrwith(movie_title_rating)
    corrwith_movie= pd.DataFrame(related_movies, columns=["Correlation"])
    corrwith_movie.dropna(inplace=True)
    corrwith_movie=corrwith_movie.sort_values("Correlation", ascending=False)
    corrwith_movie=corrwith_movie.join(mean_rating["rating count"])
    print(corrwith_movie[corrwith_movie["rating count"]> 50].sort_values("Correlation", ascending=False))
app.run(debug=True)

The function runs just fine

but when I run the above code using flask to run my API the following error pops up

 * Serving Flask app "__main__" (lazy loading)
 * Environment: production
   WARNING: This is a development server. Do not use it in a production deployment.
   Use a production WSGI server instead.
 * Debug mode: on
 * Restarting with windowsapi reloader
An exception has occurred, use %tb to see the full traceback.

It would be really helpful if some one could help me out with this.
Thank you!

Solution

In your function, you do not define the variable.

@app.route("/")
def recommend(movie_title):

Where is movie_title comming from?

You need to pass the information to the server, And then use it in the function. You have two option: Either you do that in the URL: @app.route("/<movie_title>") or you give the name in a json attached to the request:

@app.route("/",methods=['POST'])
def recommend():
    movie_title = request.json['title'] #if "title' contains the name

Answered By – Frayal

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published