Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Jupyter style interface for the code instead of just the terminal mode #32

Closed
goatchurchprime opened this issue Aug 21, 2017 · 7 comments

Comments

@goatchurchprime
Copy link

The Jupyter interface is really good because it's gives a really productive interface that's half-way between using the python immediate command line and writing and rerunning scripts.

Webrepl links to a 6000 line terminal emulator library term.js -- which is a lot of trouble to make the browser look and feel like an old fashioned unfriendly terminal.

But instead of this we could have a very similar interface to the jupyter system with a series of boxes where the code is uploaded when you press "run" by bracketing it with Control-E and Control-D, as I've done in this experiment below. (I've had to put in a delay between each line because otherwise it chokes).

To make this efficient, does anyone know what are the limitations of this mysterious Control-E / Control-D type of block? Is it saving it all in the RAM before processing it (otherwise how could Control-C work to end processing), or does it stream it to a temporary file and then import it?

<input type="button" id="runblock1" value="run" onclick="runcodeblock('codeblock1'); return false;" />
</form>
<textarea id="codeblock1" cols="80" rows="10"></textarea>
codeblockSeries = []; 
function runcodeblockSeries() {
    if (codeblockSeries.length != 0) {
        ws.send(codeblockSeries.shift()); 
        ws.send('\r'); 
        setTimeout(runcodeblockSeries, 50); // send one line at a time with a delay so it doesn't choke
    } else {
        ws.send(String.fromCharCode(4));  // Control-D
    }
}
function runcodeblock(codeblockn) {
    if (ws !== undefined) {
        data = document.getElementById(codeblockn).value; 
        codeblockSeries = data.split("\n"); 
        ws.send(String.fromCharCode(5));  // Control-E 
        setTimeout(runcodeblockSeries, 50); 
    }
}
@goatchurchprime
Copy link
Author

Possibly an even quicker way to get here is to build a new Jupyter kernel that works like a gateway and sends and receives the cell contents from the Jupyter front end and relays it to the Micropython microcontroller (if it's connected) through the USB or over the wifi.
http://jupyter-client.readthedocs.io/en/latest/kernels.html

It should be able to interpret those special % commands to handle the connections, as well as especially if some of the cells could represent code to be saved into a py or mpy module. This would mean your whole app could be developed speedily and interactively.

By using the standard IPython notebook system we've got a way to publish it on github to see the workings of a micropython session in a nice format, as we do for so many other python systems already.

@pfalcon
Copy link
Contributor

pfalcon commented Aug 22, 2017

Great ideas! Consider putting them into reality and maintaining this solution for the community!

@goatchurchprime
Copy link
Author

I'll do what I can, but I'm not very good at this sort of thing.

I'm starting this by forking a small bash_kernel implementation which I have been hacking extensively in order to find out how it works. (print statements don't work, you've got to keep a copy of stderr in a variable and write to that).
https://github.com/goatchurchprime/esp8266upy_kernel

The second step is to steal some functions from the webrepl_cli code to implement jupyter's do_execute() function and get a barely functional implementation in order to see how much I like it.
https://ipython.org/ipython-doc/3/development/wrapperkernels.html

(I'll report progress here, if I make any. I'm still kind of hoping that I'm not the first person to think of this obvious idea, and someone's already got the solution stashed away somewhere I haven't found it yet.)

@goatchurchprime
Copy link
Author

I have found this idea has been successfully implemented here (subject to a small number of issues)
https://github.com/adafruit/jupyter_micropython_kernel

I have been using for a few hours, and it is a most satisfactory way to hack code interactively. If developed further, I can imagine this becoming a really popular alternative to webrepl.

@pfalcon
Copy link
Contributor

pfalcon commented Sep 9, 2017

Glad that you found what you looked for. As a hint, you may find that http://forum.micropython.org/ is a better place to search/post about anything but the bugreports (the purpose of this tracker).

Please don't forget to close this ticket if it's "done".

@goatchurchprime
Copy link
Author

I think I've nailed it here: https://github.com/goatchurchprime/jupyter_micropython_kernel

It primarily got inspired by the script https://github.com/micropython/micropython/blob/master/tools/pyboard.py on which https://github.com/adafruit/jupyter_micropython_kernel is based, and makes use of the ctrl-A no-echo paste "raw-repl" mode, of which there is not much documentation, and may be subject to change:

https://github.com/micropython/micropython/blob/4a93801c12898898744131f57d79cf216d0861b3/ports/esp8266/help.c#L47

One obscurity with it is when you do ctrl-D it writes "OK" then does the output, then "\x04" then writes any errors (if there are any), then does "\x04>".

When you have dependent projects outside the main project, and the main project changes an obscure mode (like raw-repl) and breaks it, or requires new features so that the dependent project works better, it is not obvious whether it would be welcome to post tickets about what could be improved.

Anyhow, "Comment and close"

@Niko-La
Copy link

Niko-La commented Nov 8, 2018

@goatchurchprime It would be nice to have a docker image that builts the whole thing for jupyter lab...
great work

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants