Webpage with Javascript executing Python script with arguments
Question: I have three files in the same directory. One is a python script, which takes
argumenet. One is a html page with javascript. And the last one is a source
.wav file.

    
    
    ./myfolder/sound_manipulation.py
    ./myfolder/volume_slider.html
    ./myfolder/the_song.wav
    

The sound_manipulation.py file can be executed like:

    
    
    python sound_manipulation.py -v 50
    

and it generates a new wav file, new_song.wav, based on the_song.wav, but only
has 50% of the original volume level.

On the other hand, the volume_slider.html contains a slider goes from 0 to
100%, and a button calling an onclick javascript function, update_vol();

So far, the update_vol() alerts the value of the slider, and that's all.

    
    
    function update_vol() {
        var vol = document.getElementById('vol_slider').value;
        alert(vol);
    }
    

But I want the update_vol() to actually execute the python script using the
vol.

How can I make that happen?

Also, when the "python sound_manipulation.py -v 50" is executed, how can I
return the location of the new_song.wav back to volume_slider.html?

Please help. Thanks!


Answer: the simplest and crudest one-time cgi script might solve your problem.

set up a cgi script/environment to just get `volume` value from user / then
use `subprocess` module to process the .wav file and send it back to user. if
you need anything more than that, build your own web app.

    
    
    import cgi
    import subprocess
    import sys
    form = cgi.FieldStorage()
    volume = form.getfirst('volume') #read from form 'volume'
    
    subprocess.call(['python', 'sound_manipulation.py', '-v', volume])
    
    with open('new_song.wav', 'rb') as wav_file:
        print("Content-Type: audio/wav\n")
        sys.stdout.write(wav_file.read())
    

