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

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:

Related

Does JavaScript compile to binary?

Is JavaScript running on top of web browser?
Like Java running on top of JVM?
Or Does it actually compiled to binary code and run on machine?
V8 (in Google Chrome) contains a JS interpreter and a JIT (Just-in-time) compiler. JS code is converted to V8-specific bytecode. The bytecode is initially interpreted by the interpreter, called "Ignition". When a function becomes "hot" (it is run a lot), the TurboFan JIT compiler produces optimized machine code from the bytecode.
Other modern JS engines use similar strategies. So JS can be interpreted or compiled to machine code (using a JIT compiler), similar to how JVMs work, yes.
Javascript isn't truly compiled - it's interpreted on the browser, so yes it effectively "runs on top of the browser" on the client side.
EDIT: I should've started by saying at it's base level. As is mentioned on a comment to this, there are more complex engines now.
It has to -- nothing can run on a computer without being the appropriate machine code for the processor.
V8 converts the JavaScript to its own byte code, then is heavily optimized and converted into machine code.
Even the JVM does something similar. The JVM converts the Java byte code into machine code.

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.

Hiding javascript files from public [duplicate]

This question already has answers here:
How do I hide javascript code in a webpage?
(12 answers)
Closed 8 years ago.
I have a site whereby I can currently use firebug to look at all the angularjs files.
Is there anyway I can hide these so people cannot use tools to view them?
No you can't, the browsers need to see the code to execute it.
No. JavaScript files will always be accessible from client tools like IE dev tools or Chrome or just about any other browser's dev tools. You can take steps to make the js harder to read, but it will still be accissible
Yes, but the way is to stop people from accessing your site, which is probably worse. The way JavaScript works on most sites ( other than ones built on Node ) is that it is run in the browser and to do that it must be sent to the browser.
The reason it doesn't matter is that nobody wants to steal your code.
You need to remember that nothing you want to be kept secret should be in that JavaScript- run that on the server and pass the results out to front end - and for production you should probably be minimising your code anyway which provides a lot of obfuscation, but in general it is not worth worrying about.
You cant. But you can minify them using for example Google's closure compiler or Microsoft Ajax Minifier. Minifying not only makes your javascript less in size, but also gives an obfuscation to your code, so it is much more harder to understand

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

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

Kotlin compiler to JavaScript in JavaScript?

Is there a Kotlin compiler to JavaScript available in JavaScript (like CoffeeScript or Coco)?
If not, when is expected to be available?
Currently there's no such implementation.
The existing compiler is written in Java. It is not very clear if we (JetBrains) will work on another implementation in the nearest future. On the other hand, we are always open to contributions...

Categories