text: """""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
  This module controls the workflow of the movie recommender. 
  It ties all the other scripts together and produces the web application.
  """""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
  import random
  import logging
  import pandas as pd
  from flask import Flask
  from flask import render_template
  from flask import request
  from recommending_engine import get_recommendations, dataframe_updater
  from knn_recommending_engine import get_recommendations_knn
  
  logging.basicConfig(filename='RecommenderLog.log',
                      level=logging.WARNING,
                      format='%(asctime)s: %(message)s')
  
  MOVIES = pd.read_csv('../data/raw/movies.csv')
  df_final = pd.read_csv('../data/preprocessed/df_final.csv')
  MOVIE_IDS_LST = df_final.columns.tolist()
  
  app = Flask(__name__)
  
  @app.route('/')
  def main_page():
      five_ids = random.sample(MOVIE_IDS_LST,5)
      five_titles = []
      for id in five_ids:
          five_titles.append(MOVIES[MOVIES['movieId']==int(id)]['title'].iloc[0])
      
      return render_template('main.html',
      title='🎬 The Statistically Significant Movie Recommender 🎬',
      subtitle="Courtesy of Laura Bartolini, Behzad Azarhoushang & Francesco Mari",
      subsubtitle="who won't get offended if you don't take their advice...even if you should!",
      movie1=five_titles[0],
      movie2=five_titles[1],
      movie3=five_titles[2],
      movie4=five_titles[3],
      movie5=five_titles[4])
  
  
  @app.route('/recommender')
  def rec_page():
      html_form_data = dict(request.args) # to collect the data from the user (to build the recommendation)
      names = list(html_form_data.keys())
      
      logging.warning(f'A new user inserted new ratings for the NMF: {html_form_data}.')
  
      counter = 1
      for name in names:
          new_key = f'movie_{counter}'
          html_form_data[new_key] = html_form_data.pop(name)
          counter = counter + 1
  
      recs, new_user = get_recommendations(html_form_data,names)
      logging.warning("New NMF recommendations generated based on the user's input.")
   
      
      dataframe_updater(new_user)
      logging.warning("The movie rating generated by the user's input have been added to 'data/preprocessed/df_final.csv'.")
  
      return render_template('recommender.html', movies = recs)
  
  @app.route('/knn_recommender')
  def knn_page():
      knn_html_form_data = request.args['rating1'] # to collect the data from the user (to build the recommendation)
      
      knn_recs, orig_movie = get_recommendations_knn(knn_html_form_data) 
      logging.warning("New KNN recommendations generated based on the user's input. No update possible to 'data/preprocessed/df_final.csv'")
  
      
      return render_template('knn_recommender.html', movies = knn_recs, input=orig_movie)
  
  
  if __name__ == "__main__":
      app.run(debug=True, port=5000)      