main.py 975 B

1234567891011121314151617181920212223242526272829303132
  1. #!/usr/bin/env python3
  2. import pandas as pd
  3. from pandas.api.types import is_bool_dtype, is_numeric_dtype
  4. from nicegui import ui
  5. df = pd.DataFrame(data={
  6. 'col1': [x for x in range(4)],
  7. 'col2': ['This', 'column', 'contains', 'strings.'],
  8. 'col3': [x / 4 for x in range(4)],
  9. 'col4': [True, False, True, False],
  10. })
  11. def update(*, df: pd.DataFrame, r: int, c: int, value):
  12. df.iat[r, c] = value
  13. ui.notify(f'Set ({r}, {c}) to {value}')
  14. with ui.grid(rows=len(df.index)+1).classes('grid-flow-col'):
  15. for c, col in enumerate(df.columns):
  16. ui.label(col).classes('font-bold')
  17. for r, row in enumerate(df.loc[:, col]):
  18. if is_bool_dtype(df[col].dtype):
  19. cls = ui.checkbox
  20. elif is_numeric_dtype(df[col].dtype):
  21. cls = ui.number
  22. else:
  23. cls = ui.input
  24. cls(value=row, on_change=lambda event, r=r, c=c: update(df=df, r=r, c=c, value=event.value))
  25. ui.run()