nicegui icon indicating copy to clipboard operation
nicegui copied to clipboard

Introduce ui.get to query the element tree

Open rodja opened this issue 1 year ago • 7 comments

Originated in https://github.com/zauberzeug/nicegui/discussions/1923#discussioncomment-7419532, this PR introduces ui.get to retrieve elements of the current page with a query language inspired by tortoise-orm's filter or BeautifulSoup's find_all. Therefore it also introduces a ~~.tags~~ .keys property on Element with wich the developer can set a list of queryable words.

When complete this PR should allow these kinds of operations:

buttons = ui.get(type=ui.button)
headers = ui.get(key='header')  
action_buttons = ui.get(type=ui.button, key='action')
buttons_with_text = ui.get(type=ui.button, text='Click me')
buttons_witout_text = ui.get(type=ui.button).without(text='Click me')
buttons_within_header = ui.get(type=ui.button).within(key='header')
buttons_within_header_or_footer = ui.get(type=ui.button).within(key=['header', 'footer'])

The object which is returned by ui.get is called Elements (eg. group of elements) and allows setting props,classes,style on all containing elements at once:

ui.get(key='important').classes('text-bold')

And it allows iterating over all elements:

for element in ui.get(key='important'):
    print(element)

If type= is used the iterator automatically casts to this type:

for stepper in ui.get(type=ui.stepper):
    stepper.next()

ToDos

  • [x] type=
  • [x] iterating with typing
  • [x] .classes
  • [x] .style
  • [x] .props
  • [x] text=
  • [x] key= and .keys
  • [x] .within(type=
  • [x] .within(key=
  • [x] .exclude(type=
  • [x] .exclude(key=
  • [x] .exclude(text=
  • [x] .not_within(type=
  • [x] .not_within(key=
  • [x] .key with string or List
  • [ ] ~~.type with class or List~~ simplify usage of Mixins
  • [x] .text with string or List
  • [ ] docstrings
  • [x] documentation_get.py with demos
  • [ ] looking through the examples and checking where ui.get would make sense
  • [ ] discuss general impact on maintenance etc.

rodja avatar Oct 31 '23 06:10 rodja