Streamlit brick no longer working with App.run()

I had previously developed some Apps using the Streamlit brick. None of them appear to be working any longer.

It appears there is a conflict when using the App.run() function with the Streamlit brick.

Here is code to replicate the issue:

from arduino.app_utils import App
from arduino.app_bricks.streamlit_ui import st

st.title("Signal Error Below")

# Commenting out App.run() solves issue

App.run()

The error I am getting is:

ValueError: signal only works in main thread of the main interpreter

File "/app/python/main.py", line 7, in 
App.run()
~~~~~~~^^
File "/usr/local/lib/python3.13/site-packages/arduino/app_utils/app.py", line 101, in run
self.loop(user_loop)
~~~~~~~~~^^^^^^^^^^^
File "/usr/local/lib/python3.13/site-packages/arduino/app_utils/app.py", line 122, in loop
signal.signal(signal.SIGTERM, handle_sigterm)
~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.13/signal.py", line 58, in signal
handler = _signal.signal(_enum_to_int(signalnum), _enum_to_int(handler))

When using a userloop in App.run, I am getting the same error - there is no easy fix for this case, I'll have to rewrite all the logic.

I seem to recall, perhaps incorrectly, that previously the streamlit brick was it's own docker container? When I check "docker ps" now, I only see the python-apps-base container running.

IMO the Streamlit brick is far superior to the HTML brick, as it require no additional JS, HTML, or CSS to be written/handled - it's quick and easy to build a webUI app - and I really hope this WebUI Streamlit Brick is not being deprecated.

Also, it is worth noting, the documentation for AppLab says:

App.run()

Every Python script must include the

App.run()

function at the very bottom of the file to start the App. This command initializes App utilities and launches any imported Bricks. Note that the App framework ignores any code placed after

App.run()

In order to make relevant information available to any who are interested in this subject, I'll share a link to the formal report @programming_electronics_academy submitted to the developers of the Arduino Bricks:

It looks like the regression was introduced here:

Until such time as the Arduino developers prepare a fix and make a new release of the arduino package, the best workaround I can think of is to "monkey patch" arduino.app_utils.AppController.loop to the version of the method prior to the introduction of the regression:

import time

from arduino.app_utils import AppController
from arduino.app_bricks.streamlit_ui import st

# Source: https://github.com/arduino/app-bricks-py/blob/ffa6eb61883475d0dc974ca27ddadd93da1fda26/src/arduino/app_utils/app.py#L105-L124
def app_controller_loop(self, user_loop: callable = None):
    try:
        if user_loop:
            while True:
                user_loop()
        else:
            while True:
                time.sleep(10)
    except StopIteration:
        logger.debug("StopIteration received from user loop")
    except KeyboardInterrupt:
        logger.debug("KeyboardInterrupt received")

# Monkey patch arduino.app_utils.AppController.loop as workaround for arduino/app-bricks-py#236.
AppController.loop = app_controller_loop
App = AppController()

st.title("Signal Error Below")

# Commenting out App.run() solves issue

App.run()

Excellent approach of providing a minimal Python script to reproduce the fault!

I don't believe this was the case. If you check the Git revision history for the Docker containers of the Arduino App framework, you can see that special handling for Apps using the "WebUI - Streamlit" Brick (machine identifier arduino:streamlit_ui) was present in the python-apps-base container from the verry start:

https://github.com/arduino/app-bricks-py/blame/release/0.9.0/containers/python-apps-base/run.sh#L125-L126

Thanks @ptillisch!

If I understand correctly, what you have done is taken the older loop implementation (when the Streamlit Brick was working) and used it to redefine the code change introduced by the PR #172.

This is me thinking out loud for my own benefit, please do not feel the need respond...

My Python/Streamlit/Linux experience is not that deep, but from what I am learning, the PR introduced a Signal monitor - so the loop would stop if linux sends a signal that says "hey, stop!"

It seems these signals can only be monitored by the main thread in a python script. Since Streamlit creates children threads and reruns the main.py script (including App.run()), then a thread other then the main thread was invoking signal, which is throwing the error.

That is correct. A better term than "redefine" in this context is "revert". The "monkey patch" reverts the code back to the working version.

In case it will be helpful in understanding the subject, here is a simple example of "monkey patching" a Python class method:

class Foo:
    def bar(self):
        print("Hello, world!")


# Replacement for `Foo.bar` method.
def bar_patch(self):
    print("I've been monkey patched!")


Foo.bar = bar_patch # Monkey patch `Foo.bar`.

foo = Foo() # Instantiation of patched `Foo` class object.
foo.bar()

Thanks for this! Love it!