I have a python script that gets called from some javascript code, that I have running. And when the python script is finished I want a call back to go to a Java program of mine, I was trying to make a html page and then check with that, but I am running the Java program locally and can't connect to the FTP?
How could this be accomplished?
Thanks
EDIT
Here is how the flow works
Java calls a javascript function in browser on my local machine ->
Javascript calls python with POST Request on my Server ->
I want a callback to Java to know when Python is done ->
So that Java can move on
Do you see know how it works?
If you are running from the command line you could use pipes | which will call the next command in line with the standard out of the first command. As follows:
firstCommand | secondCommand
So if your Python script writes to standard out you could invoke it from the command line, and have the Java execute from the command line also as the second command receiving it's input from standard in.
Related
I have a question that has been asked several times here and other places around the internet*, but answers I've seen are incomplete or ineffective.
I would like to have a JavaScript function runPy() that, upon being called (in a browser, via a button-click for instance), will execute a Python script in my server that we'll call test.py.
Let's say that test.py is simply designed to create a text file and write in it 'hello world'
Python
f = open('test.txt', 'w+')
f.write('hello world')
Based on other answers, I have pieced together the following JavaScript/jQuery function:
JavaScript
function runPy() {
$.ajax({
type:'POST',
url:'test.py',
success: function(data) {
console.log(data)
};
});
}
Now, this is of course incorrect. As one might expect, rather than running the Python script, it prints to the console the contents of the script:
Console
f = open('test.txt', 'w+')
f.write('hello world')
How would I go about editing my JavaScript (and/or Python) to achieve the functionality I would like? In a perfect world, I would like to avoid importing any new dependencies (I've seen some answers dependent on Flask or Django) but of course beggars can't be choosers.
Additionally, if I will allow myself to get greedy, it would be very nice to be able to pass arguments to the Python script as well, or even use JavaScript to call a specific function in the Python script, and have the results passed back to the client-side JavaScript.
*Similar Questions
Call python function from JS
how to call python script from javascript?
Run Python scripts from JavaScript functions
You're going on the right path.
But, here's why the POST isn't working. Except for HTML filetype, making a POST call to fetch a static file on the server (i.e. random.js, random.css etc) will print the raw file content.
In this scenario, you'll need to handle this on the server-side backend code. I don't know which backend framework you're using but here are some articles that can help you with this:
NodeJS: https://medium.com/swlh/run-python-script-from-node-js-and-send-data-to-browser-15677fcf199f
.NET: https://medium.com/better-programming/running-python-script-from-c-and-working-with-the-results-843e68d230e5
Java: Running a .py file from Java
UPDATE!!: Due to the recent developments in the space of Web Development, it is now possible to run Python on the Client-side through WebAssembly. Please find more instructions here: Pyodide
There is a python module 'python-shell', but it calls the whole python program what if I just want to call a specific function of python from nodejs, is there any way ?
Thank you !
Write a short python program calling your function, and run it using the child_process module as shown in this thread. You'll be able to get standard output back.
I made a website where I can write code into a textfield and I want to send this code to my nodes.js server and call R, which is installed on my server, and make it process the written R code.
What I have trouble with is, how can I start R on my server and input code into it via Javascript?
The security aspects are not relevant for this because it will never go live and will always stay localhost.
Any ideas on how to approach this problem?
If you use an R web framework, you can then use the eval function to run the contents of the text box as code.
Obviously, this is very risky, but you've said you only want this to run locally.
You could use other languages with an exec function to spawn a child process. Write the code to file, compile with exec, then run with the exec function.
In node you could use a child process to achieve this
https://nodejs.org/api/child_process.html
In PHP you could use exec to the same end
http://php.net/manual/en/function.exec.php
All of these options carry the same risk.
If you want to safely execute R code on the server, you could implement an RPC mechanism, a simple one would consist of the name of the command and the parameters to be passed. This way, you can effectively whitelist what is allowed to execute, but it does mean you can't excecute arbitrary R code.
So I have a simple php file with the following code in it:
$contents = file_get_contents("path/to/file.html");
var_dump($contents);
Within the html file I have a few scripts that run and returns data to console.log. But, when i execute the php command (linux virtual machine) and run the file it just returns the static html content. Whereas when I open the file from the browser, it executes all the javascript files and returns the expected html output with data in console.log. To be specific, the html file executes a jasmine test suite.
So, I would like to know if there is any way to print out the results from the console.log or actually execute the javascript files through a php script and a linux virtual machine. Also, the main purpose is to execute the test via Bamboo, a continuous integration server.
I have tried phantomjs but it results in other errors and I would prefer not to use it.
Simple saying, no, you cannot do that.
The reason is that you need browser to execute javascript, and PHP just does not have browser capable of doing that as efficient as you want. There is CURL, but it cannot run javascript.
I currently wrote the following task in Javascript, but I would like to execute the same task using python cgi. Do you have any tips to perform that with python cgi?
<a href"#" onClick="parent.Content.location='https://snu.ac.kr/music.html'; return false;">Music</a>
The task is basically as follows: when a user clicks the word "Music", it shows the html page (i.e. 'https://snu.ac.kr/music.html'). The reason that I would like to perform the task using python cgi is that I would like to execute other taks combining with the current task.
Would you please let me know if there is any way to perform the above task using python cgi?
CGI is not good (as far as i read from internet) it will run a new python process everytime someone will run the script! and this is not good for your server!
maybe FastCGI is better (it will run a long single process that will handle request)