I have a file recommender.py, which contains a python function get_recommendations(title)
I want to use this in my script.js file
// Function to handle rating submission
function submitRating(movieTitle) {
const ratingInput = document.getElementById(`rating-${movieTitle}`).value;
// Store rating in dictionary
userRatings[movieTitle] = parseFloat(ratingInput);
// Log userRatings for testing (remove this line in production)
console.log('User Ratings:', userRatings);
// Call a function to handle further processing (if needed)
handleRatingSubmission();
}
// Function to handle further processing after rating submission
function handleRatingSubmission() {
var userRatingsArray = Object.entries(userRatings);
// Sort the array based on the values
userRatingsArray.sort(function(a, b) {
return a[1] - b[1]; // Change this if values are not numeric
});
console.log(userRatingsArray)
function getRatingByTitle(title) {
for (var i = 0; i < userRatingsArray.length; i++) {
if (userRatingsArray[i][0] === title) {
return userRatingsArray[i][1];
}
}
// Return null if title not found
return null;
}
var highestmovies = userRatingsArray.slice(0, -2);
if((getRatingByTitle(highestmovies[1])-getRatingByTitle(highestmovies[0]))>3){
return get_recommendations(highestmovies[1])
}
else{
var arr1=get_recommendations(highestmovies[1]).splice(0,4)
var arr2=get_recommendations(highestmovies[0]).splice(0,3)
arr1= arr1.concat(arr2)
uniqarr= Array.from(new Set(arr1))
uniqarr=uniqarr.splice(0,5)
return uniqarr.sort(function(a, b) {
return a[1] - b[1];
});
}
}
I want to use the python get_recommendations function in the Javascript code
How do I go about this? I am not familiar with Javascript at all, any help will be appreciated
I tried to use AJAX request, and converting the entire python code to Javascript but it did not work as expected.
New contributor
shravya karna is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.