1

I am trying to use NiceGUI to create a website using NiceGUI's default text input code. This code works well, however, I need to globally expose the variable e.value however I failed to using a simple "=".

I attempted to set e.value as a variable using this code however it failed, giving me a syntax error.

Here is the code I used:

from nicegui import ui
i = ("")
ui.input(label='Text', placeholder='start typing',
         on_change=lambda e: (e.value = (i))
         )
result = ui.label()

ui.run()

Any help is appreciated.

Falko
  • 17,076
  • 13
  • 60
  • 105

2 Answers2

1

Variable binding was introduced in version 1.20 via dictionary binding. Here's the documentation: https://nicegui.io/documentation/bindings#bind_to_variable

from nicegui import ui

i = ''
ui.input(label='Text', placeholder='start typing',
         on_change=lambda: ui.notify(f'{i = }')).bind_value_to(globals(), 'i')
    
ui.run()
Falko
  • 17,076
  • 13
  • 60
  • 105
riamds
  • 23
  • 5
0

Unfortunately there is (almost) no way to make assignments in Python's lambda statements. But you can simply define a regular function to do that:

i = ''

def handle_input(e):
    global i
    i = e.value

ui.input(label='Text', placeholder='start typing', on_change=handle_input)

Update: See @fam's answer for how to do it with NiceGUI >=1.20

Falko
  • 17,076
  • 13
  • 60
  • 105
  • I tried using this however 'i' remained as empty even after entering a value – user21406771 Mar 23 '23 at 21:11
  • @user21406771 How do you know? How did you check the value of `i`? – Falko Mar 24 '23 at 03:18
  • I tried using ui.label to print I however it printed nothing, also when I hovered over it vs code said Literal = ''. This was with a value entered to the text box – user21406771 Mar 24 '23 at 12:54
  • @user21406771 If you have `label = ui.label(i)` somewhere in your code, the label won't automatically update when `i` changes. You need to update the text, e.g. with `label.text = e.value`, or use something like [bindings](https://nicegui.io/reference#bindings). (It seems like you're used to Streamlit. But this is not how Python usually works. See [here](https://github.com/zauberzeug/nicegui/discussions/21) in case my guess is right and you'd like to know more.) – Falko Mar 24 '23 at 13:02
  • It tried this method but I received a syntax error. I had never used Streamlit before, but after trying it, it seems to have the same issue – user21406771 Mar 26 '23 at 20:00
  • Ok, without seeing the code it's hard to give advice. Maybe we can continue the discussion on a Gist. – Falko Mar 27 '23 at 04:57
  • Here is the code: from nicegui import ui i ='' def handle_input(e): global i i = e.value ui.input(label='Text', placeholder='start typing', on_change=handle_input) ui.run() – user21406771 Apr 03 '23 at 13:01
  • @user21406771 Yes, that's the code from my answer. But you said "It tried this method". Show what you tried. And it's probably best to add the code to your question instead of squeezing it into the comments. – Falko Apr 03 '23 at 13:47