Sending to, and receiving from, Python

I have a little webapp project in mind and trying to decide between using Meteor and Flask. I’m wondering how one achieve the following with Meteor and how easy it would be

I want to set up a server and web front end where I can upload python scripts, have the server execute the code, and update the webpage with 1) whatever python prints to the console, 2) any results that get returned, and 3) any graphs that get generated.

In a bit more detail:

Imagine if I give access to the webapp to a friend and they upload a python script. Lets say for this hypothetical situation that 1) they are fully trustable (no malicious code) and 2) I can’t control how they write their code. Once the server detects an uploaded python script it will execute it.

Now, given I can’t control what the code is, there may or may not be Python print statements in there, and may or may not be graphs generated. I can’t tell in advance, but either way I want to display those print statements and graphs on a webpage as soon as the script executes that line of code. Furthermore, if a file gets saved to disk I want to offer a download link to that file.

There will likely be times where multiple scripts get uploaded. In this situation, I need some form of persistence and queuing. The first script to be uploaded will be the first to get executed. I want the results/returns/prints/graphs of these scripts to be persistent on the webpage until the user deletes the job.

In my mind, persistence suggests a database of some kind. However, is that even useful given I have no idea in advance whether any information actually gets printed from the script? And even if so, I don’t know the structure of that data, and I control the python code so I cant tell that user to insert into a mongo document whenever they want to print (I want them to just be able to use print statements regularly). Maybe a better way would be to save all of this stuff to files on disk, and have meteor continuously monitor a directory for changes, then pull from the files for web display?

How easy is this to do for Meteor? Are there any resources you could direct me to that would help with the specifics?

I guess where I get hung up is the lack of structure in the python scripts. I’m not sure how to handle the print data and to pull it directly from the terminal/kernel (same with graphs), and then to keep it persistent without knowing what might get printed (if anything)

I guess to simplify the question, I’m wondering how meteor can be used to access the python or ipython terminal and pull that information to a webpage in real time?

I guess it depends on the python terminal and if it can print to stdout? You can run Node’s exec to run a shell command. For example I use this to run a Java library to fill out a PDF.

Here’s an example in node:

var child_process = require('child_process');

// exec: spawns a shell.
child_process.exec('ls -lah /tmp', function(error, stdout, stderr){
  console.log(stdout);
});

Here’s the example of the PDFTK wrapper in Meteor (uses the wrapAsync):
https://github.com/MeteorPackaging/pdftk/blob/master/pdftk-wrapper.js

and here’s the docs for more detailed info:
https://nodejs.org/api/child_process.html

Hope this helps!

1 Like