jczic / MicroWebSrv2

The last Micro Web Server for IoTs (MicroPython) or large servers (CPython), that supports WebSockets, routes, template engine and with really optimized architecture (mem allocations, async I/Os). Ready for ESP32, STM32 on Pyboard, Pycom's chipsets (WiPy, LoPy, ...). Robust, efficient and documented!
https://github.com/jczic/MicroWebSrv2
MIT License
659 stars 97 forks source link

Support of pyend instruction for execution of commands right after page serving #68

Open ftylitak opened 3 years ago

ftylitak commented 3 years ago

This commit adds support of a new instruction {{ pyend }}. {{ pyend }} behaves exactly as the {{ py }} in the manner of including code that will to be executed. Its only specialty is that the code included in the instruction, will be executed in a separate thread after the "generated" html from pyhtml has been served.

Usage: we use it in a microcontroller Web based UI for easy configuration. During the final step of the configuration wizard, the configurations are saved and the device gets rebooted to apply new configuration. In the {{ pyend }} we add the code to wait for a period of time and then power off/on the controller. Its benefit is that the pyhtml page includes a success message, which is first served and then the device gets rebooted, enhancing User Experience.

image

The alternative of not using this, would be either not to show a success message or to load a new page that would include the reboot code in a {{ py }}.

    {{ pyend }}
      import machine
      import utime

      start_time = utime.ticks_ms()
      WAIT_UNTIL_REBOOT_MSEC = 3000
      while (utime.ticks_ms()-start_time < WAIT_UNTIL_REBOOT_MSEC):
              utime.sleep_ms(1000)

      machine.reset()
    {{ end }}

We do not know if this is something that you consider useful though we have been using this for a while and we thought it would be better to share.