nicegui: `ui.codemirror` not updating when value changes programmatically
Description
Using nicegui==1.4.28
from nicegui import ui
shared_memory = {
'code': '// Write your code here',
'design': '',
'test': '',
}
@ui.page('/')
def main():
editor = ui.codemirror(language='python').bind_value(shared_memory, 'code')
ui.markdown(shared_memory["code"]).bind_content(shared_memory, 'code')
ui.button('clear code').on('click', lambda: shared_memory.update(code=''))
ui.run()
When the button is clicked the first time, it works as expected. Both the markdown text and the codemirror editor are cleared.
However, when it’s clicked the second time, the markdown gets cleared while the codemirror remains the same.
When I try to edit what’s in codemirror, I get the following error.
Traceback (most recent call last):
File "/Users/kexunz/miniconda3/envs/ad/lib/python3.11/site-packages/nicegui/events.py", line 413, in handle_event
result = handler(arguments) if expects_arguments else handler()
^^^^^^^^^^^^^^^^^^
File "/Users/kexunz/miniconda3/envs/ad/lib/python3.11/site-packages/nicegui/elements/mixins/value_element.py", line 40, in handle_change
self.set_value(self._event_args_to_value(e))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/kexunz/miniconda3/envs/ad/lib/python3.11/site-packages/nicegui/elements/codemirror.py", line 294, in _event_args_to_value
new_value = changeset.apply(self.value)
^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/kexunz/miniconda3/envs/ad/lib/python3.11/site-packages/nicegui/elements/codemirror.py", line 322, in apply
raise ValueError('Cannot apply change set to a document with the wrong length')
ValueError: Cannot apply change set to a document with the wrong length
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Comments: 16 (6 by maintainers)
@evnchn You’re right, this issue is kind of important and I’d love to get it fixed. I had it on my list and looked into it from time to time. It’s just that I really struggled to wrap my head around it. I’ve seen your PR, but something didn’t feel right. I suspected there must be a better, simpler solution. Like with PR #4625 where we could remove the line in question in the end.
Now I gave it another shot and came up with a rather simple PR #4635 which could replace your PR #4586. What do you think?
Thanks for reporting this bug, @zkx06111! It is indeed very similar to #3217. Therefore I’ll close this as duplicate and add a
ui.codemirrorexample over there.