Webpage with Javascript executing Python script with arguments - javascript

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!

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())

Hm, well, I guess you're out of luck. Browser scripts can't execute anything (so no Python script) on your system. As I'm sure you can imagine, this could present a huge security risk -- that's exactly how much trouble Microsoft's ActiveX got into.
I'm assuming you want to create a graphical interface for that script, so alternatively you could :
Do a JavaScript equivalent of your Python script if you absolutely need HTML
Or create an interface in QT, GTK or the like. Python can easily do that, by the way!

Related

Spawning Python process with Javascript

My goal is to run python code that the user would write on the website. I found out that spawn() could do it.
var process = spawn('python',["./script.py"] );
However, I do not want user to store their code in a file but rather i want their code to be executed directly. I want to take their code as a string and do something like this.
var process = spawn('python',pythonCodeString );
This method would obviously not work because spawn() takes file path as an argument. Are there other methods of executing user's python code with js?
P.S. I am making a website where one can edit an image using python code. For ex. user uploads an image and would want to change it to gray scale(all these transformation users can do with python)
You could just automatically create the .py file from the inputted python code/string and then subsequently call it with spawn() and delete it once it has been executed, much like a "temporary" script, so that the user doesn't need to store it manually inside a file.

Safe way to share file with data between java and javascript

I have a file containing a very small amount of data which is being updated every 10 ms by my java program.
Would it be safe to read that file simultaneously in my javascript program?
It depends on your operation system and the reading/writing software that accesses the file. If the file is locked because you try to access it in the very small time window while it is written, your read could fail. In that case you simply should build a loop, that tries again to open the file until it has success.
More about file locking: https://en.wikipedia.org/wiki/File_locking
Instead you could also use a socket or a database.

Call Python script from local JavaScript App

so I've looked around quite a bit now and wasn't able to find quite the use case I think I am confronted with.
For some background:
I'm fairly new to JavaScript and have never had to call any other program/script from it. Now I did develop a Python script that pulls some data from online sources, formats it and dumps it into JSON files. In order to display this data in a proper way I figured I would use Electron.
While handling the JSON files is completely fine (would be quite sad if it wasn't I guess), I need to be able to call the Python script updating the data from my Electron UI. As everything is local, I hoped, that there would be an easier way, than setting up some server for the Python script to run on, just to be able to trigger its execution from my Desktop App. This is especially true, as I don't even need to get or process any returns, I just want to trigger the execution of that script.
So the question now is: is there such an "easy" way to execute Python scripts from an Electron/JavaScript based locally saved Desktop app?
Thanks in advance for any answers!
Like a previous commenter mentioned, you should be able to follow this SO answer in Node.js (which is what Electron uses).
To expound upon that answer just a bit, I'd recommend using the built-in Python JSON utility to dump JSON to the standard out (just printing out the JSON string), and the using the built-in Node.js JSON utility to parse that JSON string into a javascript object for use in your application.
Alright, so after being redirected to this thread, which I can only recommend reading through if you have an interest in this issue, I took their solution and altered a little, which took me a bit of time, due to some confusion, which I now would like to spare you guys!
To re-introduce the issue: The goal is to call a python script from a JavaScipt/Electron based UI. The python script only needs to be executed, but it needs to happen onClick, as it is an update function.
Now this is the code I used:
const exec = require("child_process").exec;
function triggerUpdateAndRefreshFooter() {
exec('python relativePathToScript/update.py',
function(error, stdout, stderr) { //callback function, receives script output
refreshFooter(); //don't use the output, but I could here
}
)
}
I did have some issues figuring out all of that const stuff in the other thread, as well as having to guess IF I could just execute my script in a separate function. In the end this did work!
I hope this was helpful!

Using the `runScript` function to run a JXA script does not allow parameters

I use JXA to script workflows for Alfred 2 and recently tried to run a script from within another script. I need to pass some text between the scripts, so I decided to use parameters, but whenever I try to pass a string, a number, an array or anything else that isn't an object to it, it gives the error "Error on line 4: Error: An error occurred.". If I do pass an object, the second script (the one being run by the first script) receives an empty object rather than the one passed to it. The same happens when the first script is an AppleScript, but if the second script is an AppleScript, it all works perfectly. Passing arguments through osascript from the command line also works. Is the API broken or is there something that I'm doing wrong?
First script:
var app = Application.currentApplication();
app.includeStandardAdditions = true;
app.runScript(new Path("/path/to/second/script.scpt"), { withParameters: "Hello World!" });
Second script:
function run(args) {
return args;
}
Edit:
If the second script is edited as below, the dialogue is displayed but the runScript method of the first script still returns an error.
function run(args) {
var app = Application.currentApplication();
app.includeStandardAdditions = true;
app.displayDialog(args.toString());
return args;
}
Edit 2:
The runScript function actually seems to be working perfectly other than the problem with the parameters. The error isn't actually being thrown, just displayed by the Script Editor, and execution continues after the call to runScript as if nothing had happened. The returned value also work perfectly, despite the parameters not working.
A note about Alfred 2 workflows
To run some code in Alfred 2 (triggered by a search, keyboard command, etc.), it must be typed into a box in the app, not in a file.
The box to enter code in is very small and does not provide syntax highlighting, and this makes editing code difficult and annoying. For smaller files, it is okay, but for larger files it is easier to use a short script to run a script file. I've tried Bash, which would be the simplest option, but Alfred 2 does not provide an option to escape single quotes. I also cannot use script libraries (to my knowledge, correct me if I'm wrong), as the code is not in a script bundle and all of the required files need to be within the same folder (for exportation reasons).
I don't know how to avoid the runScript error, but I can suggest an alternative approach: load the script as a script library.
Using a script library
Turning a script into a library can be as simple as saving the script to ~/Library/Script Libraries. If your script file is named script.scpt and has a run handler, and you save it to the Script Libraries folder, then you can then invoke it from another script like so:
Library("script").run(["Hello, world!"])
Script libraries are documented in the JXA release notes for OS X 10.10, in the WWDC 2014 session video introducing JXA, and in the AppleScript Language Guide.
Embedding a script library inside of a script bundle
According to the AppleScript Language Guide documentation for script libraries, there is a search policy for finding Script Libraries folders. The first place it searches is:
If the script that references the library is a bundle, the script’s bundle Resources directory. This means that scripts may be packaged and distributed with the libraries they use.
To apply this to the example given in the question, you would need to re-save the first script as a script bundle, and then embed the second script inside of the first script.
For example, if you re-save the first script as script.scptd, then you could save the second script embedded.scpt to script.scptd/Resources/Script Libraries/embedded.scpt. You should then be able to use Library('embedded') to access the script library.
To re-save an existing script as a script bundle, you can either use the File > Export... menu item in Script Editor, or you can hold down option while selecting the File menu to reveal the File > Save As... menu item. The File Format pop-up menu lets you choose the Script bundle format.
Once you have a script bundle open, you can reveal the bundle content panel by using the Show Bundle Contents menu item or toolbar button. You can then use the gear menu to create the Script Libraries folder inside of the Resources folder, and then you can drag a script into that folder.

How to parse html that includes javascript code

How does one parse html documents which make heavy use of javascript? I know there are a few libraries in python which can parse static xml/html files and I'm basically looking for a programme or library (or even firefox plugin) which reads html+javascript, executes the javascript bit and outputs html code without javascript so it would look identical if displayed in a browser.
As a simple example
link
should be replaced by the appropriate value the javascript function returns, e.g.
link
A more complex example would be a saved facebook html page which is littered with loads of javascript code.
Probably related to
How to "execute" HTML+Javascript page with Node.js
but do I really need Node.js and JSDOM? Also slightly related is
Python library for rendering HTML and javascript
but I'm not interested in rendering just the pure html output.
You can use Selenium with python as detailed here
Example:
import xmlrpclib
# Make an object to represent the XML-RPC server.
server_url = "http://localhost:8080/selenium-driver/RPC2"
app = xmlrpclib.ServerProxy(server_url)
# Bump timeout a little higher than the default 5 seconds
app.setTimeout(15)
import os
os.system('start run_firefox.bat')
print app.open('http://localhost:8080/AUT/000000A/http/www.amazon.com/')
print app.verifyTitle('Amazon.com: Welcome')
print app.verifySelected('url', 'All Products')
print app.select('url', 'Books')
print app.verifySelected('url', 'Books')
print app.verifyValue('field-keywords', '')
print app.type('field-keywords', 'Python Cookbook')
print app.clickAndWait('Go')
print app.verifyTitle('Amazon.com: Books Search Results: Python Cookbook')
print app.verifyTextPresent('Python Cookbook', '')
print app.verifyTextPresent('Alex Martellibot, David Ascher', '')
print app.testComplete()
From Mozilla Gecko FAQ:
Q. Can you invoke the Gecko engine from a Unix shell script? Could you send it HTML and get back a web page that might be sent to the printer?
A. Not really supported; you can probably get something close to what you want by writing your own application using Gecko's embedding APIs, though. Note that it's currently not possible to print without a widget on the screen to render to.
Embedding Gecko in a program that outputs what you want may be way too heavy, but at least your output will be as good as it gets.
PhantomJS can be loaded using Selenium
$ ipython
In [1]: from selenium import webdriver
In [2]: browser=webdriver.PhantomJS()
In [3]: browser.get('http://seleniumhq.org/')
In [4]: browser.title
Out[4]: u'Selenium - Web Browser Automation'

Categories