# Update textbox content from other thread

**URL:** <https://discuss.huggingface.co/t/update-textbox-content-from-other-thread/23599>\
**Category:** 🔒 Gradio\
**Created:** [September 25, 2022, 7:30pm UTC](https://discuss.huggingface.co/t/update-textbox-content-from-other-thread/23599 "2022-09-25T19:30:20Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![Barricade](https://avatars.discourse-cdn.com/v4/letter/b/b38774/32.png) [@Barricade](https://discuss.huggingface.co/u/Barricade)\
**Post date:** [September 25, 2022, 7:30pm UTC](https://discuss.huggingface.co/t/update-textbox-content-from-other-thread/23599/1 "2022-09-25T19:30:20Z")

</div>

Hello, I’m started with gradio with the following simple code example:

```auto
import gradio as gr
from threading import Timer

time = 1

class RepeatTimer(Timer):
    def run(self):
        while not self.finished.wait(self.interval):
            self.function(*self.args, **self.kwargs)

def timer():
    global time
    time += 1 
    box = demo.children[0].children[0]
    box.value=str(time)

with gr.Blocks() as demo:
    box = gr.Textbox(value="start", label="timer", interactive=True)    

# create & start timer thread
t = RepeatTimer(2.0, timer)
t.start()

# start web server
demo.launch()   

```

Why I get no update of the textbox content ? In the debugger I see that the value is updated.

What can I do ?

---

<div class="post-metadata">

**Author:** ![freddyaboulton](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/freddyaboulton/32/32732_2.png) [@freddyaboulton](https://discuss.huggingface.co/u/freddyaboulton)\
**Post date:** [September 25, 2022, 8:43pm UTC](https://discuss.huggingface.co/t/update-textbox-content-from-other-thread/23599/2 "2022-09-25T20:43:42Z")

</div>

Hi @Barricade ! Thanks for the question.

If I understand correctly, you’re trying to periodically run a function to update the UI of your app?

There isn’t an api to do that in gradio right now. See [Make it easier to work with streaming data · Issue #2050 · gradio-app/gradio · GitHub](https://github.com/gradio-app/gradio/issues/2050). In general, updates to your app have to be triggered through interacting with the UI.

Do you mind sharing a bit more about your use case?

---

<div class="post-metadata">

**Author:** ![Barricade](https://avatars.discourse-cdn.com/v4/letter/b/b38774/32.png) [@Barricade](https://discuss.huggingface.co/u/Barricade)\
**Post date:** [September 25, 2022, 8:58pm UTC](https://discuss.huggingface.co/t/update-textbox-content-from-other-thread/23599/3 "2022-09-25T20:58:36Z")

</div>

Hi @freddyaboulton,

thank you for checking my question. At first, I saw the possibilities from gradio to build very easy web gui applications with python. But my first use case is a web based heating house controller 🙂 running on a raspberry pi with python in a docker container. There are different temperatures, different relay actors, i want show the values in the web gui. Next step is to define times for switching on and off the relays and I want do it in home from a tablet and from outside with a smartphone.

“In general, updates to your app have to be triggered through interacting with the UI.”

Is it f.e. possible to send post requests (from a thread over localhost) to the web gui ?  
Or has the gui thread a running hookable function ?  
Or can I simulate button click events, from an unvisible button ?

---

<div class="post-metadata">

**Author:** ![freddyaboulton](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/freddyaboulton/32/32732_2.png) [@freddyaboulton](https://discuss.huggingface.co/u/freddyaboulton)\
**Post date:** [September 26, 2022, 4:02pm UTC](https://discuss.huggingface.co/t/update-textbox-content-from-other-thread/23599/4 "2022-09-26T16:02:14Z")

</div>

Very cool demo @Barricade !

To show the latest values of the temperature, you do not need to manually update the config like you did in the demo. The config only reflects the state when the app is created.

To read the latest temperature value and display it in the UI you can use the `load` event listener. There is a rest api for all gradio apps. When you create an event listener, you can use the `api_name` parameter and that will automatically document a rest endpoint to trigger that event.

So a think a skeleton of your demo can look like this:

```python
def get_temperature(...):
    ....

def turn_off_relay(....):
    ....

with gr.Blocks() as demo:
    temperature = gr.Number()
    relay_id = gr.Dropdown(choices=[...])
    off_switch = gr.Button(value="Turn off Relay")
    off_switch.click(turn_off_relay, inputs=relay_id, outputs=None, api_name="turn_off")
    demo.load(get_temperature, inputs=None, outputs=[temperature])

demo.launch()

```

You can then click on the “view api” button at the bottom of your app to the the “turn\_off” endpoint.

![image](https://us1.discourse-cdn.com/hellohellohello/original/2X/f/fa39392c827426da965acde061e25c858a790c3f.png)

![image](https://us1.discourse-cdn.com/hellohellohello/original/2X/7/7bf90c91c836a21f72c9c9c88a1a182ab22b37dd.png)

If you plan on turning them on or off on a schedule, you can use the code you have to run the “turn off” logic in a separate thread but you shouldn’t store the state of whether or not a relay is turned on or off in the app itself but rather in an external datasource and have gradio from that instead.

Hopefully that helps!

---

<div class="post-metadata">

**Author:** ![Barricade](https://avatars.discourse-cdn.com/v4/letter/b/b38774/32.png) [@Barricade](https://discuss.huggingface.co/u/Barricade)\
**Post date:** [September 27, 2022, 10:54am UTC](https://discuss.huggingface.co/t/update-textbox-content-from-other-thread/23599/5 "2022-09-27T10:54:08Z")

</div>

Hi Freddy,

thank you for your help, i started with your example, using visual studo code as development environment, view python printf output in the terminal window from vs code.

```auto
import gradio as gr

gTemperature = -10

def get_temperature():
    global gTemperature
    gTemperature += 1
    print('temperature:', gTemperature)
    return gTemperature

def turn_off_relay(id):
    print('relay:', id)
    return

with gr.Blocks() as demo:
    temperature = gr.Number()
    relay_id = gr.Dropdown(choices=['Rel 1', 'Rel 2'])
    off_switch = gr.Button(value="Turn off Relay")
    off_switch.click(turn_off_relay, inputs=relay_id, outputs=None, api_name="turn_off")
    demo.load(get_temperature, inputs=None, outputs=[temperature])

demo.launch()

```

The “demo.load” function is working, but not as hoped by me 🙂  
The function get\_temperature() was called only one time at starting the app and  
than allways i press the F5 key in the browser (temperature is going higher).

I wished, that allways the temperature is changing, the value in the number widget is automatily change, like a thermometer.

```auto
PS D:\Projects\python-projects\gradio> & python.exe d:/Projects/python-projects/gradio/update-textbox.py
Running on local URL: http://127.0.0.1:7861

To create a public link, set `share=True` in `launch()`.
temperature: -9
temperature: -8
relay: Rel 1
relay: Rel 2

```

For testing the API I installed in firefox the “RESTClient” plugin, whrere I can fire post and get requests with JSON content, but I’m not family with the syntax.

---

<div class="post-metadata">

**Author:** ![freddyaboulton](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/freddyaboulton/32/32732_2.png) [@freddyaboulton](https://discuss.huggingface.co/u/freddyaboulton)\
**Post date:** [September 27, 2022, 4:46pm UTC](https://discuss.huggingface.co/t/update-textbox-content-from-other-thread/23599/6 "2022-09-27T16:46:24Z")

</div>

Hi @Barricade!

> [@Barricade](#):
>
> The function get\_temperature() was called only one time at starting the app

Right now, it’s not possible to automatically update the UI when the backend data changes. I suggested using the `load` event so that you could get the latest temperature whenever you manually refreshed the page.

We’re thinking about how to support this use case though! We filed this [issue](https://github.com/gradio-app/gradio/issues/2050) (linked above) to track this.

---

<div class="post-metadata">

**Author:** ![Barricade](https://avatars.discourse-cdn.com/v4/letter/b/b38774/32.png) [@Barricade](https://discuss.huggingface.co/u/Barricade)\
**Post date:** [September 28, 2022, 9:57am UTC](https://discuss.huggingface.co/t/update-textbox-content-from-other-thread/23599/7 "2022-09-28T09:57:09Z")

</div>

ok, api tests are now working, with:

curl -X POST [http://127.0.0.1:7861/api/turn\_off](http://127.0.0.1:7861/api/turn_off) -H ‘Content-Type: application/json’ -d ‘{“data”: [“Rel 1”]}’

the above function turn\_off\_relay(id) was called

---

<div class="post-metadata">

**Author:** ![freddyaboulton](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/freddyaboulton/32/32732_2.png) [@freddyaboulton](https://discuss.huggingface.co/u/freddyaboulton)\
**Post date:** [September 30, 2022, 4:45pm UTC](https://discuss.huggingface.co/t/update-textbox-content-from-other-thread/23599/8 "2022-09-30T16:45:02Z")

</div>

Hi @Barricade!

We’ve been working on adding an API to gradio apps that lets you run functions forever and not refresh the page.

What we have right now is the `run_forever` method. It’s similar to the `load` method but like it says but it runs indefinitely without refreshing the page. For your temperature use case you can do something like

```python
def get_current_temp():
    .... .....

with gr.Blocks() as demo:
    temperature = gr.Number()
    demo.run_forever(get_current_temp,
                     inputs=None,
                     outputs=temperature,
                     every=1 # units in seconds)
demo.queue().launch()

```

It’s available as a python wheel here: [https://gradio-builds.s3.amazonaws.com/run-forever/v1/gradio-3.4-py3-none-any.whl](https://gradio-builds.s3.amazonaws.com/run-forever/v1/gradio-3.4-py3-none-any.whl)

An example using this feature is here: [nvidia-smi - a Hugging Face Space by julien-c](https://huggingface.co/spaces/julien-c/nvidia-smi)

This is an experimental feature - the api might change but we’d love to get your feedback on the current API.

---

<div class="post-metadata">

**Author:** ![Barricade](https://avatars.discourse-cdn.com/v4/letter/b/b38774/32.png) [@Barricade](https://discuss.huggingface.co/u/Barricade)\
**Post date:** [October 1, 2022, 7:26am UTC](https://discuss.huggingface.co/t/update-textbox-content-from-other-thread/23599/9 "2022-10-01T07:26:30Z")

</div>

Hi @freddyaboulton,

thank you, your example is working, also if i change your example from

```auto
every=1 # units in seconds)

```

to

```auto
every=0.1) # units in seconds

```

The gui is updating very quickly.

(next text removed, found my mistake)

---

<div class="post-metadata">

**Author:** ![Barricade](https://avatars.discourse-cdn.com/v4/letter/b/b38774/32.png) [@Barricade](https://discuss.huggingface.co/u/Barricade)\
**Post date:** [October 6, 2022, 12:49pm UTC](https://discuss.huggingface.co/t/update-textbox-content-from-other-thread/23599/10 "2022-10-06T12:49:23Z")

</div>

Is there a possibility to have more than one “run\_forever” functions ?

Yes, i checked is working very well.

---

<div class="post-metadata">

**Author:** ![freddyaboulton](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/freddyaboulton/32/32732_2.png) [@freddyaboulton](https://discuss.huggingface.co/u/freddyaboulton)\
**Post date:** [October 6, 2022, 5:35pm UTC](https://discuss.huggingface.co/t/update-textbox-content-from-other-thread/23599/11 "2022-10-06T17:35:58Z")

</div>

Great to hear @Barricade ! Will try to release this formally soon. Thank you for testing 🙂

---

<div class="post-metadata">

**Author:** ![Barricade](https://avatars.discourse-cdn.com/v4/letter/b/b38774/32.png) [@Barricade](https://discuss.huggingface.co/u/Barricade)\
**Post date:** [October 14, 2022, 7:38am UTC](https://discuss.huggingface.co/t/update-textbox-content-from-other-thread/23599/12 "2022-10-14T07:38:08Z")

</div>

Hi, can you inform here please, when the for me very well working feature is gone in the release ?

---

<div class="post-metadata">

**Author:** ![freddyaboulton](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/freddyaboulton/32/32732_2.png) [@freddyaboulton](https://discuss.huggingface.co/u/freddyaboulton)\
**Post date:** [October 14, 2022, 4:54pm UTC](https://discuss.huggingface.co/t/update-textbox-content-from-other-thread/23599/13 "2022-10-14T16:54:49Z")

</div>

Absolutely @Barricade! Have not released it yet but planning to do it soon.
