You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Dear PyGui does not actually do any sorting for you. The table API is a more general purpose API and it is up to the library user to handle sorting. To sort a table based on user interaction, you must assign a callback to the table. This callback will be ran when a user tries to sort a table by clicking on the table's column headers.
The second argument of the callback contains information on the sorting specifications to use. The following scenarios are possible:
no_sorting -> 2nd argument = None
single sorting -> 2nd argument = [[column_tag, direction]]
Where direction is 1 for ascending or -1 for descending.
With this information, the library user can reorganize the table using standard Dear PyGui functions i.e.:
move_item(...)
reorder_items(...)
Example
For this example, we only consider 1 column but this is easily extended:
importdearpygui.dearpyguiasdpgdpg.create_context()
dpg.create_viewport()
dpg.setup_dearpygui()
defsort_callback(sender, sort_specs):
# sort_specs scenarios:# 1. no sorting -> sort_specs == None# 2. single sorting -> sort_specs == [[column_id, direction]]# 3. multi sorting -> sort_specs == [[column_id, direction], [column_id, direction], ...]## notes:# 1. direction is ascending if == 1# 2. direction is ascending if == -1# no sorting caseifsort_specsisNone: returnrows=dpg.get_item_children(sender, 1)
# create a list that can be sorted based on first cell# value, keeping track of row and value used to sortsortable_list= []
forrowinrows:
first_cell=dpg.get_item_children(row, 1)[0]
sortable_list.append([row, dpg.get_value(first_cell)])
def_sorter(e):
returne[1]
sortable_list.sort(key=_sorter, reverse=sort_specs[0][1] <0)
# create list of just sorted row idsnew_order= []
forpairinsortable_list:
new_order.append(pair[0])
dpg.reorder_items(sender, 1, new_order)
withdpg.window(label="Tutorial", width=500):
withdpg.table(header_row=True, borders_innerH=True, borders_outerH=True,
borders_innerV=True, borders_outerV=True, row_background=True,
sortable=True, callback=sort_callback):
dpg.add_table_column(label="One")
dpg.add_table_column(label="Two", no_sort=True)
foriinrange(25):
withdpg.table_row():
dpg.add_input_int(label=" ", step=0, default_value=i)
dpg.add_text(f"Cell {i}, 1")
# main loopdpg.show_viewport()
whiledpg.is_dearpygui_running():
dpg.render_dearpygui_frame()
dpg.destroy_context()
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
Answer
Dear PyGui does not actually do any sorting for you. The table API is a more general purpose API and it is up to the library user to handle sorting. To sort a table based on user interaction, you must assign a callback to the table. This callback will be ran when a user tries to sort a table by clicking on the table's column headers.
The second argument of the callback contains information on the sorting specifications to use. The following scenarios are possible:
[[column_tag, direction]]
[[column_id, direction], [column_id, direction], ...]
Where direction is 1 for ascending or -1 for descending.
With this information, the library user can reorganize the table using standard Dear PyGui functions i.e.:
move_item(...)
reorder_items(...)
Example
For this example, we only consider 1 column but this is easily extended:
Resources
Beta Was this translation helpful? Give feedback.
All reactions