Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to call ui.table methods with return values in NiceGUI 1.0 #230

Closed
3 tasks
falkoschindler opened this issue Jan 6, 2023 Discussed in #229 · 2 comments
Closed
3 tasks

How to call ui.table methods with return values in NiceGUI 1.0 #230

falkoschindler opened this issue Jan 6, 2023 Discussed in #229 · 2 comments
Assignees
Labels
enhancement New feature or request
Milestone

Comments

@falkoschindler
Copy link
Contributor

falkoschindler commented Jan 6, 2023

Discussed in #229

Originally posted by johancj January 6, 2023
Hi, how can I use methods in ui.table with a return value in nicegui version 1.0?

I would really appreciate an example like in #150, but with for example the function getSelectedRows. #150 is very similar to my case, but not applicable for nicegui v1.0


The undocumented possibility to receive return values from AG Grid API calls has been lost when leaving JustPy behind for version 1.0. It would, however, be a valuable feature and not too hard to implement, since we already support return values from ui.run_javascript.

  • support return values for ui.run_method
  • support return values for table.call_api_method
  • demonstrate getSelectedRows on API reference
@falkoschindler falkoschindler self-assigned this Jan 6, 2023
@falkoschindler falkoschindler added the enhancement New feature or request label Jan 6, 2023
@falkoschindler
Copy link
Contributor Author

I'm still hesitating to implement return values for run_method and call_api_method as initially planned. Both would have to become async and, like with run_javascript, we would need to introduce parameters respond, timeout and check_interval. That's possible, but a rather significant API change, especially switching from sync to async.

In contrast to the original plan, I found a rather simple alternative: Introducing getElement() in JavaScript in 23da696, we can now write something like:

result = await ui.run_javascript(f'getElement({table.id}).gridOptions.api.getSelectedRows()')

This keeps the API for NiceGUI's core features nice and simple while enabling power users - especially of 3rd party modules like AG Grid and Highcharts - to get full access of underlying APIs.

Here is a complete example for trying out different API methods with and without return value:

table = ui.table({
    'columnDefs': [{'field': 'name'}],
    'rowData': [{'name': 'Alice'}, {'name': 'Bob'}],
})

async def count():
    rows = await ui.run_javascript(f'getElement({table.id}).gridOptions.api.getSelectedRows()')
    ui.notify(f'{len(rows)} selected')

ui.button('Select', on_click=lambda: table.call_api_method('selectAll'))
ui.button('Deselect', on_click=lambda: table.call_api_method('deselectAll'))
ui.button('Count', on_click=count)

@falkoschindler falkoschindler added this to the v1.1.1 milestone Jan 12, 2023
@maxim-trokhimtchouk
Copy link

Thanks guys. I ended up implementing an async version of run_method on our fork to solve this problem. However, this is a simple enough workaround and I much rather stick to the official fork.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants