How to run python code from javascript code using xampp? - javascript

I have a website written in html and I have a button that when clicked should run a python script. The button is programmed in javascript, and I have seen on this site how to connect javascript and python together, but the problem is that I'm running the website using xampp and localhost, so I don't know the configurations to run the python code. I have tried to use up mod_wsgi, but the instructions are very confusing and I really have no idea what I'm doing.
I don't want to use JQuery or Ajax, I'd rather use just regular javascript to run the python code. How would I accomplish this? Basically I just want the button to run a python script I have, which is in the same directory as the website is. I don't want to run the python code from localhost directly, rather I want to reference and run it from the javascript code for the button.
Edit: it has been pointed out to me that JavaScript and python are connected through a client server relationship. Therefore my question is how to run the python code through a webserver from xampp.
Thanks in advance

Related

Java/Python Cross-Compiling in JavaScript

I am working on a project that involves having a Java code editor and a Python code editor but I have no way of compiling that code. I've used Ace within my html and I have no idea how to compile the Java code or Python code using JavaScript. I currently have a way of retrieving all of the Java/Python code to a string. From there I need a way of compiling that code so that I can run it on my website and test it. There will be no GUIs involved, all of the Java/Python code will just have console output. However, I need a way I can run the Java/Python code on my website in live speed. Everything must be done on the website, the client shouldn't have to download anything extra. I am basically trying to replicate the website 'codingbat.com'. Thanks for the help in advance.
Python would usually get JITted when you invoke a good Python interpreter (ha, the name does not apply). Java has some great compilers (standard javac or the Eclipse JDT) so I would not even think of compiling with some other language.
Where does the need come from to cross-compile in JavaScript? It sounds like you are searching for a Java compiler implemented in JavaScript, and likewise for Python.
If everything has to be built and deployed to some website, why don't you create a script (shell, JavaScript, or a Jenkins Pipeline) that compiles the java part using javac, precompiles the python part as required and deploys the output directly to your website?

Runnning python script on HTML page

I would like to basically call a python script from HTML, after the script is called and it finished running, I would like to execute a javascript file(wich I know how to do.) Now my question is: Can I do this with just pure HTML and javascript or do I need to get a library for python? If I dont need a library, how would I go about doing this?
You can use Two Python libraries.
Django
Flask
I recommend Django. Django is easy and fast to make.
the Flask is more complex but you can make more detail functions.
Can I do this with just pure HTML and javascript?
You might want to execute python on the browser, then look at something like this http://www.skulpt.org/, but the most common use case is that no, you need to execute javascript on the browser client-side and python on server-side
So no, you need something that executes python in a web server (apache/nginx) context, like
gunicorn (standalone/wsgi)
uwsgi (standalone/wsgi)
mod_python (apache module)
Then you would execute like this for example
browser ---http---> server(nginx) ---wsgi---> python_server(uwsgi)+library(Django)
<-------------/ <------------/

Stress testing a JAvascript file which only runs in the browser

I need to run a Javascript file for a relatively long time(maybe about 5 weeks or so at one time), without it stopping or being interrupted. Currently the script is a client side script which connects to the server and receives data via Sockjs. There is no HTMl/GUI. There is only some computation.
I need to make sure the client always stays connected to the server all the time. I need to be able to run some script from the command line which is like forever.js. I have tried porting the javascript to node.js but it doesnt work. It only works in the browser. I have tried reading the file and doing an eval but none of it works. Are there any other options open for me? I have tried phantomJS but that doesnt work too. I have looked at How can I use a javascript library on the server side of a NodeJS app when it was designed to run on the client? and Load "Vanilla" Javascript Libraries into Node.js but I reapeatedly get SockJS is not defined. I guess the problem lies deep in the library and is not a simple fix.
Could anyone give me some pointers? What are my other options? Whats the best way to test a client javascript library which seems to work only on the browser?
This is the repo I am using :
https://github.com/sockjs/sockjs-client
It doesnt seems to be running on node. I tried to replace the script tag with require, and download the sock.js into a separate file and use it.
There are "headless" browser modules available. These produce a virtual browser environment that can be programatically controlled. The primary use of these is to do unit/integration testing of browser side code without actually running a browser:
Phantomjs
Slimerjs
These might fit your needs. You can create a nodejs script that will load the said code in a virtual browser page.

Call python from javascript [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Calling Python from JavaScript
I have a test.py and test.js.
I want to be able to run my test.py by opening test.js. I don't know how to create an api because it's not a web app, it's just 2 files sitting on my linux mint desktop.
I don't want to use npapi, because it's just a simple task, i don't want to use pyjamas because it's too hard to install the pyjamas desktop, so how to do it?
please note that i can use php, ajax, jquery instead of javascript, if it could be done with these languages. I am also able to use C++ or C instead of python. I just want to know a simple way to do it.
I know this can be done if i use java instead of python, but i want to know if i can do it with python, C or C++.
If you put your test.py file where ever you put your cgi files, (I think it's /usr/lib/cgi-bin by default for apache on ubuntu linux), you should be able to run the python file just by making a request to it's address.
If you just want to run the python file, and not use its output in the browser afterward,
you could probably get away with something like:
document.write('<img src="http://localhost/cgi-bin/test.py" />');
if you want to use the output in the browser, you will probably be best served by using jQuery or some other library to do an easy ajax call.
something like:
jQuery.get('/cgi-bin/test.py', function(data) {
//do stuff with the data
})
would probably do you just fine.
the second method would require that you also use apache, or equivalent, to serve the test.js file from localhost because jQuery ajax generally requires requests to go to the same domain that the script is running on.

Is it possible to use a web page as a user interface to a program written in python, running locally, without a web server?

I have a program written in python, and I would like to make it easy to enter parameter values for this program through a GUI. I realise that I could create a GUI using python tools, but I am interested in using a html / javascript page and have the javascript code call my python script when the user clicks a button to run. Something like;
var xmlhttp = new XMLHttpRequest();
xmlhttp.open("GET", "../scripts/python_script.py", true);
xmlhttp.send();
Currently, when I do that, I just get back the text in the python script, but it doesn't actually run. Ideally the python script would run in the background without blocking further input to the web page, and as the script produces it's different result files (png images), these would be displayed in the browser. Clearly, I could do this using a web server (and I may end up doing this eventually anyway, hence the html interface), but I am wondering if it is possible to do so without one. That way I could package the html page and the python script together and give them to someone who could then go and run the program on their computer without needing to start a web server. Is this possible?
If it is not, is there an alternative way do achieve a similar result? Could I embed a small server into a python script that displays the html page when it starts up, and then responds to an XMLHttpRequest to start the python script? If I did this, would the user have to start the script, and then go to the specified address in their browser as a separate action?
EDIT: I got a quick solution working using SimpleHTTPServer, but I had a look at bottle and I'll probably try something using that as well. Thanks for your help.
First of all, using something like bottle it is pretty simple to make a web server to run your script. Look at http://bottlepy.org/docs/dev/
A good starting point is the code at http://bottlepy.org/docs/dev/tutorial.html#http-request-methods but you would put up a form asking for parameters rather than a login form. Then just run your Python script, capture the output and send it back in the return statement.
This question Capture subprocess output shows you two ways to run your main script depending on whether you want to show the output progressively or all at the end.
You'd need to bundle some kind of webserver with the application. If it is not intended for deployment I would go for something like bottle.py. It's a micro web framework that has its own development server. Other micro/mini frameworks probably pack their own webserver with them for development purposes (web2py, flask, ..).
If you want something more serious you'd probably need some better web server. If that's the case - have a look at this reddit discussion.

Categories