How to run your defined function in FLASK for building API


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

def recommend(movie_title):
    print("for movie({})".format(movie_title))
    print("Top 10 movies recommended are")
    corrwith_movie= pd.DataFrame(related_movies, columns=["Correlation"])
    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))

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!


In your function, you do not define the variable.

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:

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