vsolina / micropython-web-editor

Web-based code editor (IDE) for MicroPython controllers with WiFi
MIT License
27 stars 5 forks source link

Saving the main.py file do not run the new code #1

Open casainho opened 1 year ago

casainho commented 1 year ago

When I change and save (CTRL+S) the main.py file, the new code do not run.

Here is a screenshot micropython-web-editor running on a ESP32-S2 board:

image

casainho commented 1 year ago

Seems there is some information here on how to do it: https://forum.micropython.org/viewtopic.php?t=12373&p=67136#p67141

vsolina commented 1 year ago

main.py is executed after boot.py which in turn "is executed when the pyboard boots up." From official docs: https://docs.micropython.org/en/v1.9.3/pyboard/pyboard/tutorial/script.html

In order to run the 'main.py' after a change (+save) you'd need to reboot the controller (soft reboot should suffice).

If you prefer not rebooting the controller on every change - you could force it to re-execute from the python REPL (unload the main.py, load it again) - but that's a bit painful. I prefer doing it via shell, downside is that you need to install additional software (https://github.com/vsolina/mipyshell), on the upside it does allow you to create directories/files, delete them, and various other standard operations.

But I think there's a way this situation could be improved in a simple, straightforward manner: adding functionality to execute an arbitrary file which you're editing currently. If you agree I'll treat this issue as a feature request to add described feature. It might have special case for boot.py and main.py which are treated differently by MicroPython (and executed automatically).

casainho commented 1 year ago

I come from the CircuitPython webeditor, and there, there is a big button saying "SAVE + RUN". That is what I am looking for, the file to be saved on the editor, is also re-executed.

Can you do it with urgency please? I can test it right now.

vsolina commented 1 year ago

I definitely think we should still be able to Save a .py file without executing it (submodules, etc.), but I agree it would be useful to have a Run / Save+Run options too.

Yes, I'll treat this one as a high priority and implement it first.

casainho commented 1 year ago

"unload the main.py, load it again" I think is a best option, as I do not expect to loose board connection. For instance, on circuitpython, while editing the code on VisualCodeStudio, I just need to hit Save file and the code will restart - if the board would reboot, then I would need to everytime configure the board serial port, etc... that would take a lot of time!!

vsolina commented 1 year ago

Agreed, the new feature will allow you to re-execute any .python file (including main.py) without a reboot. (but save without execute feature will remain)

vsolina commented 1 year ago

This feature turned out to be tricky because MicroPython does not have APIs allowing control of Main thread from background threads. Since the Editor backend runs in a background thread, killing an active task and starting a new one is tricky. Almost impossible through any reliable means. The only way I found is by using os.dupterm() function and simulating keyboard events: KeyboardInterrupt - to kill anything that might be running; AND some python code to execute a specified file

That's how I implemented it, but it's still in a very early phase of development and might not cover all the cases. It certainly has some limitations, e.g. if your program catches KeyboardInterrupt events - stopping it won't work. But I've tested it in various situations and so far it seems to work fine. It even cooperates nicely with other active terminal duplications (e.g. WebREPL, telnet daemon, etc.)

This feature and a few others are available in the development branch: https://github.com/vsolina/micropython-web-editor/tree/development

Please let me know if you find any issues.