How can I execute a Python script from Javascript? - javascript

I have written a module in python which performs some function.
I then created a Google Chrome extension which makes use of JSON and javascript.
Now when I click on the extension I want it to execute the python program which is stored on my hard disk and display the output on the browser again.
Is there a way in which I can do this??

Repeated :
Calling Python from JavaScript
Here you have some answers, Pyjamas is probably the best option in your case

Probably a late reply but a possible solution is to make your python script act as a server and let the browser plugin interact with it.

forgive me if i'm incorrect on infinite proportions.
I believe that JavaScript is executed in a sandboxed/ isolated environment. Therefore you cannot invoke a python interpreter* or any other executable residing on the system.
*unless the interpreter itself were written in javascript.

Related

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

Windows UI Automation from a Web Page

I have VB.NET code that handles automation of various application installs. I want to move this essentially out of a VB generated EXE package, and be able to execute the same code (or the equivalent result) from an HTML page on a server. Is this possible? It looks like javascript can't cross the web application/desktop application barrier. Perhaps I can execute the VB.NET code/application that handles the automation (stored server side) from code in the HTML? This is a fairly broad question(s) so ideas are welcome. Please post examples with your ideas!
Thanks.
Ian's comment made me read the question again. If you really plan to execute code on the client through a web browser, please ignore this answer.
You can use any server-side language that you want to do this. Since you already wrote it in VB, you can use ASP.NET or PHP's exec(). Example:
<?php
if($_GET["dosomething"]) {
exec("mycommand");
}
will execute mycommand when you pass dosomething as a paramter to the file.

Writing to a System File Using Javascript

I'm working with an API with a feature that can only be accessed (easily) using Javascript, but I want to use the API to save a .txt file to my server. Is there any way to achieve this on a Mac OSX machine? I know that JS running in a browser is prohibited from doing this, so I guess this is really a two part question: (1) what's the simplest way to run a 10 line JS script on Mac OSX and (2) how would I write data to a txt file doing this?
You could go for a headless browser for example PhantomJS. I haven't used it, but it should run JS well.
The same restrictions apply though, no filesystem access from JS.
Unless you use something like the plugin framework in FF, in that case you have elevated rights for the scripts.
Besides that you could create a wrapper in php, perl or other language, and pass on the data from JS to them in an ajax call, and they write the txt for you.

Writing to a text file using Javascript

I want to write to a text file using javascript. I know that it is possible but there are some problems.
I am running a javascript program that calculates the location of an object (its latitude & longitude) which changes every 5 seconds; i want to write this information to a text file. The javascript program will soon run on a server and I'll use the information written to the text file to communicate with an Android app on my phone.
So, my question really is:
How can it be done properly?
I know that there may be some permission issues but considering it won't be online and available to others will it be a problem and, if it is, should I go with PHP to do what I want? I know ASP is more Microsoft orientated and I work with a Mac so PHP would be the preference here.
When writing a file, is it possible to write to an existing file or does the process simply destroy and recreate the same file?
I would use PHP
http://www.php.net/
This has a good code example:
http://www.tizag.com/phpT/filewrite.php
Also, you can make the request using jQuery's AJAX function, this will allow to effectively run this code from javascript:
http://api.jquery.com/jQuery.ajax/
Using the latest HTML5 Javascript, you can use the FileSystem APIs to read/write/append to text files. This is a good tutorial here : Exploring the FileSystem APIs.

Can I save a file which will be generated from php code on server side using only javascript/C/C++ and no browser opened?

What is the best way to save a file from internet on javascript and/or C and/or even C++?
I saw this same question for C# and Java, but nothing to this three languages, so here is the question.
Hey, not so easy. The url point to some http://xx.xxxx.com/p.php?pid=staticetctectc....
I guess is php code which produce a nice gif in my browser. I just want to save this gif. Without opening browser. It is possible to do with javascript/C/C++?
Most near answer I found is this.
Thanks in any advice.
You can easily do this with JavaScript using Node.js. Here is a link to an example: http://www.hacksparrow.com/using-node-js-to-download-files.html
You could also do it from the command line using wget or curl. They are both available on pretty much every platform you can imagine.
For C++, you could do it like in Downloading File in Qt From URL. If you do not want to depend on Qt, libcurl is also an option.
For JavaScript, the method described in File Download Using JavaScript should work.

Categories