This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def recommend(user_id): | |
| '''recommend N items to user''' | |
| sparse_user_item = load_npz("sparse_user_item.npz") | |
| with open('model.sav', 'rb') as pickle_in: | |
| model = pickle.load(pickle_in) | |
| recommended, _ = zip(*model.recommend(user_id, sparse_user_item)) | |
| return recommended, map_movies(recommended) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def most_similar_items(item_id, n_similar=10): | |
| '''computes the most similar items''' | |
| with open('model.sav', 'rb') as pickle_in: | |
| model = pickle.load(pickle_in) | |
| similar, _ = zip(*model.similar_items(item_id, n_similar)[1:]) | |
| return map_movies(similar) | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def map_movies(movie_ids): | |
| '''takes a list of movie_ids and returns a list of dictionaries with movies information''' | |
| df = pd.read_csv('ml-1m/movies.dat', delimiter='::', header=None, | |
| names=['movie_id', 'title', 'genre'], engine='python') | |
| # add years to a new column 'year' and remove them from the movie title | |
| df['year'] = df['title'].str[-5:-1] | |
| df['title'] = df['title'].str[:-6] | |
| # creates an ordered list of dictionaries with the movie information for all movie_ids |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def sparse_matrices(df): | |
| '''creates the sparse user-item and item-user matrices''' | |
| # using a scalar value (40) to convert ratings from a scale (1-5) to a like/click/view (1) | |
| alpha = 40 | |
| sparse_user_item = csr_matrix( ([alpha]*len(df['movie_id']), (df['user_id'], df['movie_id']) )) | |
| # transposing the item-user matrix to create a user-item matrix | |
| sparse_item_user = sparse_user_item.T.tocsr() | |
| # save the matrices for recalculating user on the fly |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| $('#contact-form-button').click(function(event){ | |
| // Prevent redirection with AJAX for contact form | |
| var form = $('#contact-form'); | |
| var form_id = 'contact-form'; | |
| var url = form.prop('action'); | |
| var type = form.prop('method'); | |
| var formData = getContactFormData(form_id); | |
| // submit form via AJAX | |
| send_form(form, form_id, url, type, modular_ajax, formData); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| {% extends "base.html" %} | |
| <form action="/contact" id="contact-form" method="POST" enctype="text/plain"> | |
| <input type="hidden" name="csrf_token" value="{{ csrf_token() }}"/> | |
| <div class="row"> | |
| <div class="input-field col s6"> | |
| <input type="text" class="validate" name ="first-name" id="first-name" pattern="[a-zA-Z]*" required/> | |
| <label for="first-name">First name</label> | |
| <span class="helper-text" data-error="Only text is accepted." data-success=""></span> | |
| </div> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var csrf_token = "{{ csrf_token() }}"; | |
| $.ajaxSetup({ | |
| beforeSend: function(xhr, settings) { | |
| if (!/^(GET|HEAD|OPTIONS|TRACE)$/i.test(settings.type) && !this.crossDomain) { | |
| xhr.setRequestHeader("X-CSRFToken", csrf_token); | |
| } | |
| } | |
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # rendering contact.html template and making JSON response | |
| from flask import Flask, render_template, jsonify | |
| # using Flask-WTF CSRF protection for AJAX requests | |
| from flask_wtf.csrf import CSRFProtect | |
| # initializing app | |
| app = Flask(__name__) | |
| # protecting our app | |
| csrf = CSRFProtect(app) | |
| @app.route('/contact', methods=['GET', 'POST']) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function modular_ajax(url, type, formData) { | |
| // Most simple modular AJAX building block | |
| $.ajax({ | |
| url: url, | |
| type: type, | |
| data: formData, | |
| processData: false, | |
| contentType: false, | |
| beforeSend: function() { | |
| // show the preloader (progress bar) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def power(a, b): | |
| """Returns arg1 raised to power arg2.""" | |
| return a**b |
OlderNewer