Call python with argument from javascript

156 Views Asked by At

I want to create a simple website to show lyrics of a songs using the genius API, the problem i encountered is that i can't find lyrics with the genius API only with javascript. So i created a simple python function to find the lyrics, the arguments of the method is the title of the song and the artist.

Python code :

import lyricsgenius

def get_lyrics(theSong,theArtist):
    genius = lyricsgenius.Genius("token")
    artist = genius.search_artist(theArtist, max_songs=1, sort="title")
    song = genius.search_song(theSong, artist.name)
    return song.lyrics

I found this to make a request to the python file :

$.ajax({
  type: "POST",
  url: "~/pythoncode.py",
  data: { param: text}
}).done(function( o ) {
   // do something
});

I know that i need a server to run the python because it's not running client side, i have a cpanel account and i can create python app on it. When you create : passenger_wsgi.py

This is what the passenger_wsgi.py contain :

import os
import sys
sys.path.insert(0, os.path.dirname(__file__))
def application(environ, start_response):
    start_response('200 OK', [('Content-Type', 'text/plain')])
    message = 'It works!\n'
    version = 'Python %s\n' % sys.version.split()[0]
    response = '\n'.join([message, version])
    return [response.encode()]

So now how do i put everything together to make this work, i just need to make a request to the python file with the name of the artist and the title of the song and then it returns me the lyrics. Thanks

0

There are 0 best solutions below