How to run user's Python and Java code in Node Js - javascript

I am creating a test case website to allow students to solve some programming questions and test their code. I am using express and Node Js.
I am planning to add multiple languages such as Javascript, Python, and Java.
For javascript, I was able to use vm2 sandbox to safely run the user's arbitrary code. Go here to see how I did it.
However, I am still not sure about how to implement it for Java and Python.
For Python, there is the build-in "child_process" that allows running a python script which is basically another python file and allows us to get the output from it. What I want is to get the user's code from a text area and run it inside the app.js file.
Is there any library or sandbox that allows me to do that?

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?

Can you run Transcrypt and JavaScript at the same time?

I need to create a program that is able to do things possible in python only (I.E. Editing local files) while also using javascript API's
Would i be able to:
A. access variables defined in Transcrypt and access them in Javascript and vice versa or
B. run functions from javascript in Transcrypt and vice versa
As far as your A & B questions, yes transpiled Python code can access JavaScript functions/variables and JavaScript can call transpiled Python code and access its variables. This actually works really well. I would recommend utilizing npm and Parcel or Webpack with the respective Transcrypt plug-in to manage the build process if you are using more than just one or two JavaScript libraries.
Note that Transcrypt is primarily intended to run code in a web browser, so you are limited to what programs can generally do in that environment - including limitations on local file access. If you are running the Javascript code in Node then, maybe.
Lastly, third party libraries are generally NOT supported (Numpy excepted via the NumScrypt project), and not all of the standard libraries have been ported yet. The exception to this, is if the third party library you want to use is pure python AND all of it's dependencies are pure python as well. There are not many that meet this qualification, as most have some dependency on a C-library somewhere in the dependency stack. Most of the Python built-ins and language constructs are available though.

Javascript frameworks just "markup" for javascript?

I am a little curious about how javascript frameworks work. Web development isn't really my area of expertise (I'm more of a c/c++ guy), but do javascript frameworks get translated into vanilla javascript?
Upon inspection of website source, it seems like it is mostly just standard javascript. Do these javascript engines just translate code into javascript on the server side?
Yes, most of JavaScript Frameworks translates the code you write to vanilla JavaScript, however, this does not happen on the Server Side, that would be really slow (Server side code is used to check databases, serve files, authenticate, etc.). This process of translation is done in compilation time (Although it is translation). (Just like when you compile c++ code into binary).
When it's source code to source code like JavaScript and React (JSX) to Vanilla JavaScript (JS), it's translation. When it's source code to binary like C++ source code to an executable (.exe) is compilation.
After you're done writting your JavaScript code with frameworks, you most translate it to Vanilla JavaScript (if you also used other uncommon languages to write styles, you must translate them too, like SASS instead of CSS). It is also common to minify it, so it can load faster.
All this is mainly done by tools like webpack.
When your site is up and running, we can say that is run time.
Looking at the fact that they were written in js they would be resolved to js before running and as Robin said they are executed on client side except Node which is a runtime environment and not a framework

Writing code for JS and Java

I'm trying to write a module of code which can be used with Javascript for the client side browser and Kotlin for a Java desktop application and Android app.
The main logic manipulates a bitmap/png file.
Is there a way which I can write an interface to be used and have different implementations of the interface for JS and Kotlin?
For example, write a wrapper class for an image (load from image, set pixels, get pixels) in Java (using BufferedImage) and JS (using Canvas)?
I'm new here so if anything doesn't make sense or needs more clarification, please let me know!
Yes, it's certainly possible to do that. This answer is a generic Java and JavaScript answer; there may also be a Kotlin-specific approach, see the link in zsmb13's comment. But the generic Java and JavaScript approach would be:
Write your interface around BufferedImage in Java
Write your interface around canvas in JavaScript
Write your manipulation code using that interface in JavaScript
On the browser, your manipulation code would run directly on the browser's JavaScript engine
In the Java app, you'd run your JavaScript code using javax.script to run it in Nashorn (or Rhino on older Java environments).
Whether it's the best solution to your problem is something only you can decide, but yes, you can do it.

How to write WebSocket output to a text file?

I would like to write the output of a JavaScript WebSocket continuously to a local text file. How can I do this?
Writing to files seem impossible for JavaScript (security reasons). That's why I am searching for another solution. This doesn't need to be a browser based solution. Windows cmd is good, too!
Well, as you seem to already know, you can't write to a local text file from regular browser Javascript. Security provisions prevent that. Here are some things you could do:
Write a browser plugin and from that you could write to the local disk.
Write a local node.js Javascript app that connects to your webSocket and then writes incoming data to a text file. Then, run that app.
Write a local app in any of your other favorite languages (Java, Python, C++, C#, etc...) that connects to your webSocket and then writes incoming data to a text file. Then, run that app.
FYI, a node.js app to do this would probably be about 20 lines of code to write.

Categories