0

I've recently finished my courses in HTML and CSS, and am now sailing into Javascript. I have a decent background in Python at the moment, and would like to utilize my experience for web programming.

My question being, how could I go about creating a terminal environment that could display my python code within my HTML page? For example, if I had a script named hello.py, and it printed a hello world message to the user, how could I take that script, and display its outcome within my page, so when you loaded it up, it would read 'hello world'?

I hope this is clear. I'm not referencing using Python to display HTML, like a framework like Django, but rather how to take a script, run it, and display its output to a web page?

  • Do you want to just run it once offline and produce pages that can be viewed or give somebody the ability to call your script from a web page and see the results. – Dave Oct 03 '16 at 20:27
  • I need the second option. I want to be able to run a small text-rpg through a browser window, and running things like a `combat` function, and updating classes, would need that capability right? – classmethod Oct 03 '16 at 20:35
  • The script will have to be executed server side. This means that you will need some other language other than HTML and JavaScript involved. You could use PHP, or if you don't feel like learning a new language you could use NodeJS (JavaScript on the server). Having said that, it is rarely a good idea to mix as many languages for simple tasks. It is probably better to stick with JavaScript, and HTML as they were designed for the web. – Rodolfo Oct 03 '16 at 20:38
  • An alternative approach would be to run a server using SimpleHTTPServer, and hit he endpoints using AJAX; however, that may become really complex really fast. – Rodolfo Oct 03 '16 at 20:54

1 Answers1

0

Leaving aside the security problems, you can do this in several ways.

You can pursue a "stateless, sort of" approach, which is simpler, by displaying a fake user prompt in CSS and supplying a textarea or input area that manages to emulate a command line. When the user hits ENTER you would get the input, send it for execution to the local shell and capture the output, then display it back formatting it as a terminal would. This could be done better in AJAX, I think.

Since you work in Python this is a related question and answer for twisted.

The above method only allows you very simple (but still dangerous :-) ) interactions. If you launched a ncurses program or any kind of interactive program which attempts to read input, you'd get immediately stuck. This is enough to run your hello.py, however.

You can improve on the method using tested libraries such as this jQuery plugin.

A more complex way would be to allocate a terminal locally (one per each user session), capture all keyboard events and "echo" them to the terminal, and read the console. You would need to know how to offer a valid terminal environment with appropriate capabilities, and then you'd need to decode the program output (complete with ANSI terminal commands) and "rebuild" the (virtual) monitor appearance, then send this "monitor" view to the client browser to be displayed somehow (AJAX, websockets...). The results would be way better, but the effort would be proportionally greater.

This latter approach (in Python) leads to projects such as this, or this, which communicates with the client through websockets.

UPDATE

All this is not needed to do what you require; you seem to need something between a module executor (not sure of this) plus a chat function (this looks mandatory).

What you need is to abstract the input and output of your program so that it can "talk" to the web browser. In this case you might run your program as a daemon (there are modules that allow daemonizing a stdio-based program). Then, the user interface (you can use the jQuery plugin) collects player A's input and sends it to the program, collects the output and sends it back to A's screen. You need to think about temporizations and asynchronicity, or turn management: player A might type in five commands while player B slowly types only one. Also, going on, you'll have to manage sessions and disconnects/reconnects of users.

Unfortunately, the "Web" world is quite different in its approaches and problems from the "keyboard" world.

Community
  • 1
  • 1
LSerni
  • 55,617
  • 10
  • 65
  • 107