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.
Related
I have a working Gulp script that I use to create my SVG sprites along with a preview page. But now I want to enrich the preview page, more precisely each icon in it, with information that I have in a separate json file. But I don't have any idea how to do this.
It should look something like this:
function taskFactory(name, spriteMode) {
return gulp.src(srcSvgDir + '*.svg')
.pipe(svgSprite(config))
.pipe(jeditor((json) => {
// read json file for this sprite
// iterate entries in json
// Find the location in preview page (by selector or placeholder string) and paste the information from the json entry.
}))
.pipe(gulp.dest(destDir));
}
But I have no idea if I even have access to this preview page that is being created. If anyone has a completely different idea, I'm open to anything.
In a case of emergency, I could write a C# console app that, after creating the sprite and preview page, parses and edits the latter file. I don't think that's so nice though, since I want to create it purely with Gulp.
Admittedly, I'm new to some of this...
Building a website on a local server. It has a ton of JS function in an external JS file.
Site has a MYSQL DB. (I am still learning this).
As part of my calculations from functions in that external JS file, I want to update and/or read from that DB.
I have been trying to read up on node.js and trying to read up on PHP (still learning both), but I'm not sure if I'm sniffing in the right direction.
Do I somehow invoke functions from node.js from the external JS file? Do I somehow invoke the PHP (in the form of a function, I suppose) from the external JS file?
How does one typically do this?
I have definitely learned that this in the external JS file does not do the trick. First window appears, but second doesn't:
// Activate the node.js library for MYSQL access
alert("got here 1");
var mysql = require('./mysql');
alert("got here 2"); // nope, this never pops up
Higher-level advice might be more useful than detailed in-the-weeds advice...? Still very new to this.
Thank you kindly!
-=-=-=-=-
self-muttering thoughts... I am using the external JS file to hold a bunch of functions that do all kinds of manipulation and conformation to the data that I collect on the front end:
<button class="ButtonOperation" onclick="DataLog(document.getElementById('DataWindow').value,'NE_AssembleOrder')">Log Data</button>
Am I eventually going to discover that I should instead port all of these functions over to a big PHP file instead?
-=-=-=-=-
Okay, took a while before I better understood this. So, this is the answer that would have gotten me moving in the right direction (for any future reference):
The thing to understand is that for this project, you want to manipulate data to and from a database, which means that (at least for now, for the sake of simplicity), the key is to get your data into a package and send it up to the server, and then have a function running on the server take up the yoke from there.
The way to do that (old school), is with a form.
When you SUBMIT a form, all that data on the form is bundled up and sent to the server.
In this instance you have an index.html page, and that page will open a new page for each of the functions you're trying to track. Use JavaScript to pop open the window and then when you include the URL for the window, pop in a Popup_SpecificFunction.php file. (change SpecificFunction as needed)
So far, so good. ;)
Now, in that Popup_SpecificFunction.php, you will collect all your data under a single form. A good ol' HTML form, with a [SUBMIT] button. That very same Popup_SpecificFunction.php file also has a reference in the header, referring to the big main library of PHP functions -- which is a file sitting on the server.
That [SUBMIT] button invokes a ProcessAllThisData function -- which is on the server-side PHP file. In doing this, it sends all the data from the form -- including a lot of data you include in hidden controls -- to the serverside function.
And at that point, you are basically "on the server" with all your data, and then you can just code that function in PHP and manipulate the database and other stuff as needed.
Using the form was the thought-jump you needed, because prior to this, you've generally thought of forms as standalone data, but they can have actions associated with the entire forms.
You can still use JavaScript to do client-side stuff, but here's another thing that can trip a person up:
There is a difference between these two HTML items as far as whether or not you should use them to send data to and from the server, or whether or not you are just going to JavaScript something on that button:
<button></button>
and
<input type="button"></input>
You might have to experiment a bit to figure out which is which.
This was everything you needed to get you moving in the right direction.
Sincerely,
Future Me. :)
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.
I have a JavaScript file on my server that contains a function. I would like to develop a REST Api to connect to this server, run the JavaScript function and send back the output.
Is it possible to call a JavaScript function from a php file?
I read this but it doesn't answer my question, because my js file is hosted on the same server as the php file.
Is the V8Js extensions what I am looking for?
Edit
The js function looks like this:
function (line, userWeight, weightunit){
//logic is here
var computed = {
userLengthFtin: userLengthFtin,
userLevel: userLevel,
proId: line['id'],
proLengthFeetin: proLengthFeetin,
proThick: proThickFtin,
weightunit: weightunit
};
return computed;
}
Is it possible to call a javascript function from a php file ?
You would need to hand things over to some other software which can execute JS. This might be through shelling out or it might be though a library such as Selenium or the V8js library you found.
Whatever you choose, it would need to be able to handle the particular needs of the JS (e.g. if the JS expects to be embedded in a webpage with access to a DOM and all the APIs provided by a web browser, then you couldn't simply run it with Node.js).
It would probably be simpler to rewrite the function in PHP.
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!