问题
OK, so I am experimenting with a simple python repl in a browser. I want to put some python code in a text input, and then hit the 'send' button, then get back the result of evaluating it.
I was using flask, with one route, which called the stdlib eval
function, and then sends it back. I was also using zeroMQ with a simple server/client setup. I feel like I am missing something though...
How is the standard way to go about doing this kind of thing? Projects like repl.it and skulpt are doing it but they are compiling to javascript. Ipython notebook is using zeroMQ, well I don't know the exact way they are doing it, sending some python string with a pub/sub setup and then using exec
I think....
I was thinking I would like to embed repl's into webpages that support many languages, but I am starting with python first. Maybe someone else is doing this already? Any help is appreciated!
回答1:
IPython notebook webapp discuss with a tornado webserver that act as a bridge with ZMQ to talk to kernels, kernels might be python, julia, haskell, OCamel, nodejs, ruby, scala ... for thoses that I know of. As long as a kernel comply to the message spec (and nice blog post about messaging protocol from the guy who wrote IHaskell) it is transparent for the frontend that just send strings/ receive results.
If you want a more stand-alone version you can look at Min's single cell demo. But basically you should be able to embed IPython's kernel.js
in a page, and just use IPython.kernel.execute(code, callbacks)
.
The exact way of how this work in the kernel differs from language to language. Python can natively do so as you can eval a buch of code in a specific namespace, for other languages I can't tell you exactly.
Instead of reinventing, I think re-using IPython's message spec and reusable component would make sens[1].
Also I've heard from reliable source that IPython want the ability to start notebook with languages in different languages, and that they also want some closer than a REPL (like QtConsole) in the browser[2].
[1] I'm biased, I'm an IPython dev.
[2] That something we often talk about, but we have some Python-ism we want to get rid of first.
来源:https://stackoverflow.com/questions/19610542/repl-in-webpage-best-way-to-go-about-it