Is it possible to run native python code in chrome? [duplicate] - javascript

This question already has answers here:
How can I integrate Python and JavaScript?
(13 answers)
Closed 13 days ago.
I was wondering if it is possible to run native python code in chrome instead of javascript.
I have seen projects like pyjs or brython, but what they do is compile python to javascript. While those are great projects a problem with this approach is always that you cannot easily debug the original python code.
Amongst other things I stumbled upon this thread, describing how to develop custom code for chromes-sandbox. But this would probably lead to implementing a python interpreter in the sandbox, which would be an overkill.
Any help is appreciated! (I don't want to start any python vs. javascript discussions, but only find out if this would be possible and if so, how to do it)
Kind Regards,
Marco

Python is a programming language, you can't run native code of a programming language. You can however, run programs written in python in the browser.
So can I run python code in the browser or not?
Update June 2013: The Brython project is now available letting you run Python 3 code in your browser. You can check it out at http://www.brython.info/ .
You can use run LLVM in JavaScript using ECMAScripten. This means you can compile c-python from C to JavaScript and just run JS in the browser. The link to the ECMAScripten wiki provides more details on how to do that.
(Here is a live demo of a python REPL running in the browser)
There also exist python 2 implementations that work in the browser.
But should I?
Probably not, JavaScript and python are similar in many things, both are dynamic, both are compact and both are clever. If you know python you can learn JavaScript very quickly.
If you like python's syntax, you might want to consider CoffeeScript which has similar syntax to Python (and somewhat to Ruby) and compiles to JavaScript transparently.
Solutions that run python in the browser will also tend to be much slower, at least until more browsers will support asm.js (currently only firefox nightly).

I believe you can create a compiler in Javascript, to run simple python code. There are probably some available programs as well that will allow this to be carried out. Although, it is not possible to run python directly through a web browser.

Now , it's possible . you can run python in browser

Related

How to run javascript snippets in firefox/chrome from a script

I am currently trying to learn javascript. Coming from a python/R background I find it really useful for learning purposes to be able to write code in a script in Rstudio/pyCharm which I can then execute in a interpreter by highlighting specific lines of code and then pressing a ctrl+enter or some other keyboard shortcut.
The console available on firefox/chrome seems incredibly rich and useful for learning / testing specific pieces of code but I find it quite limiting that I can't store each line in a script with comments/notes to myself.
Is there a way to run lines of javascript in firefox/chrome like pycharm & rstudio can with their respective interpreters or how is it usually recommend for people to learn the language in an efficient manner ?
Use node.js which is a javascript runtime using v8 (powering the chrome console)
You can get an REPL similar to Python.
You cannot, however use it to modify the DOM or access the window. In such case, using an online IDE like jsfiddle or codepen might be a good alternative.

How do I compile C++ to JavaScript in a browser?

I'm aware of Emscripten and LLVM, but neither are written in JavaScript intended for a browser.
As far as I can tell, the tools exist, but they haven't been put together, but I could very well be missing some key factor that makes it very difficult to compile C++ to JavaScript in a browser.
So I'll mention my naive implementation:
Compile Emscripten to C, then use Clang to compile it to LLVM, then use Emscripten to compile it to JavaScript.
Compile Clang to LLVM, then compile that to JavaScript through Emscripten.
Write some C++ and run it through JavaScript Clang, then run that LLVM through Emscripten to get JavaSscript
Run the resulting JavaScript!
I'm sure I'm missing something from my steps. Please let me know, and let me know if there are any efforts by the community to resolve those missing pieces.
EDIT: JSCPP is the closest I've found, but it lacks a lot of features and frankly the author took on an absurd undertaking, by trying to rewrite the parser etc. on his own, rather than using existing tools. Still, close and pretty neat.
It is possible to compile C++ to JavaScript using a self-hosting version of Emscripten that runs in a browser, such as emception.
Alternatively, it is possible to run a C++ compiler (or even an entire operating system) using an x86 emulator in JavaScript.

Load jQuery in node.js CLI [duplicate]

This question already has answers here:
Can I use jQuery with Node.js?
(21 answers)
Closed 6 years ago.
I'm an R developer by day and I'm trying to learn to do stuff with Javascript at night. I'm very used to the REPL type environment where I can run code in the IDE where I'm working. The code I'm writing is for a web application, but I'm still in the learning stage with Javascript so I want to do exploration, practice, etc. and I'd like to do it in a REPL environment so I can try something, fail, try something else, etc.
I have installed node.js and configured Sublime Text to "build" my Javascript files via node in the CLI. However, I'd like to try using some things from jQuery. Is there a way for me to load jQuery into the node.js execution environment so my standalone script can use the necessary features?
To be clear, I'm not talking about use node.js on a server, as a webserver, any of that. I'm just using node.js as an execution environment on my PC to execute vanilla and, hopefully, slightly french bean vanilla code.
I've read that you can't source in other Javascript files but obviously a web browser loads in code from multiple sources and co-mingles them so it seems like I should be able to do something similar when executing on my local machine.
If you just want to try jQuery in a REPL environment, you can do that from the dev tool from your browser (major browsers has it called 'console' under F12 usually), no need for nodejs here, JS engines are implemented in browsers.
But if you want to use the same core functionality and syntax of jQuery, you might want to try cheerio package https://cheerio.js.org it's a:
Fast, flexible, and lean implementation of core jQuery designed specifically for the server.

Can python functions run in html as javascript does? [duplicate]

This question already has answers here:
Can Python be used for client side web development? [closed]
(8 answers)
Closed 6 years ago.
How should I go about turning my JavaScript functions into Python functions so as to have my project's code structure as close as possible to what I already have? Would for instance Python functions run embedded in HTML as JavaScript functions do? Of course, I did not try that yet and I am assuming it is not that simple, but I don't understand why. Would it only be because browsers don't have Python available out of the box?
<script type="text/python">
def myFunction (x):
if x > 10:
print("x is larger than 10")
</script>
instead of:
<script type="text/javascript">
function myFunction () {
if (x > 10) {
alert("x is larger than 10");
}
}
</script>
It is not possible to run Python in any modern browser because, while theoretically possible, none contain a python interpreter. Javascript is the only language which runs in a browser without plugins like Flash or ActiveX.
One possibility for being able to write Python code that will eventually run in the browser is to use a "transpiler". This is a tool that will compile the python code into Javascript. So the browser is ultimately running the language it knows, but you're writing Python. There are already a number of languages like CoffeeScript, TypeScript and even React JSX templates that compile down to raw javascript.
An example of a Python to Javascript transpiler is Transcript. Just keep in mind, since it's not actually Python, there are no guarantees for performance or compatibility since that's largely up to how well the transpiler does it's conversion. You may start with a 2 line Python script that compiles into 30 lines of javascript in order to replicate what you're trying to do.
Below is an example of Transcript compilation:

Javascript interpreter to replace Python

In terms of quick dynamically typed languages, I'm really starting to like Javascript, as I use it a lot for web projects, especially because it uses the same syntax as Actionscript (flash).
It would be an ideal language for shell scripting, making it easier to move code from the front and back end of a site, and less of the strange syntax of python.
Is there a good, javascript interpreter that is easy to install (I know there's one based on java, but that would mean installing all the java stuff to use),
I personally use SpiderMonkey, but here's an extensive list of ECMAScript shells
Example spidermonkey install and use on Ubuntu:
$ sudo apt-get install spidermonkey
$ js myfile.js
output
$ js
js> var f = function(){};
js> f();
Of course, in Windows, the JavaScript interpreter is shipped with the OS.
Just run cscript or wscript against any .js file.
There are four big javascript interpreters currently. V8, Squirrelfish, Spidermonkey and Rhino. I think more important than performance is how well it integrates into existing infrastructure, and I guess Rhino with its bridge to Java wins here.
Try jslibs, a scripting-focused standalone JS runtime and set of libraries that uses SpiderMonkey (the Gecko JS engine).
On the 'easy to translate' theme, there's also Lua.
It's somewhat similar to Javascript, but more 'orthogonal' (closer to functional roots).
The heavy orientation to 'pure' programming theory has made it really small and fast. It's the fastest scripting language around, and the JIT runs circles around the new JavaScript JITs that are starting to appear.
Also, since it was originally thought as an extension language, it has a very nice and clean interface to C, making it very easy to create bindings to any C library you might want to access.
Google's V8 can be used as a standalone interpreter. Configuring with scons sample=shell will build an executable named shell, that can be called like so: ./shell file.js.
You'll need some server-side JavaScript interpreter. Check out the following blog post. Something such as Rhino might be useful for you.
You might try toying around with SquirrelFish or v8, both should be runnable on the command line.
FYI, there is a built-in one already on modern windows platforms. You need to use JScript, but it's close enough. Same environment also allows for VBScript. To run a program you can execute something like:
cscript foo.js
The windows system API is a bit weird and frustrating if you expect the same flexibility as with basic JS objects, but they do have thorough documentation if you can handle digging through the MSDN pages and seeing all the examples in VBScript.
Not sure what's available for Linux/Mac in terms of js shell.
Well, for safety reasons, javascript had not been provided with file access right by design. So as a scripting language, it's a bit limited.
But still, if you really want to, spider monkey is your best option. Here is a tuto :
http://developer.mozilla.org/en/Introduction_to_the_JavaScript_shell
Node.JS. It's great. Has many modules. you can do all your file scripting with Node.
In my years I've found most Javascript developers find it quite easy to transfer over to PHP and vice versa - it isn't a direct answer to your question, although if you're working in ActionScript and JavaScript then you're best to stick with something like PHP (if you're not willing to move to Java, and stick with the ECMA base)

Categories