{
"cells": [
{
"cell_type": "markdown",
"id": "6e8a319c-087b-435e-8cbd-1f4e593b728b",
"metadata": {},
"source": [
"# Edit Grid"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "d033ad47-b6b3-46b1-84ef-8a7a9c40fe18",
"metadata": {
"editable": true,
"slideshow": {
"slide_type": ""
},
"tags": [
"remove-cell"
]
},
"outputs": [],
"source": [
"%run __init__.py"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "dcd2cbe9-ec3e-47d2-b41c-391b606cdf42",
"metadata": {
"editable": true,
"slideshow": {
"slide_type": ""
},
"tags": []
},
"outputs": [],
"source": [
"import typing as ty\n",
"from pydantic import BaseModel, Field, RootModel\n",
"from ipyautoui import AutoUi\n",
"\n",
"\n",
"class DataFrameCols(BaseModel):\n",
" string: str = Field(\"string\", column_width=100)\n",
" integer: int = Field(1, column_width=80)\n",
" floater: float = Field(3.1415, column_width=70, global_decimal_places=3)\n",
" something_else: float = Field(324, column_width=100)\n",
" nullable_string: ty.Optional[str] = None\n",
"\n",
"\n",
"class TestDataFrame(BaseModel):\n",
" \"\"\"a description of TestDataFrame\"\"\"\n",
" a: str\n",
" df: ty.List[DataFrameCols] = Field([DataFrameCols().model_dump(mode=\"json\")], format=\"dataframe\")\n",
" "
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "5c4ac8e3-0518-40cf-92e7-0a7845c6bc96",
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "6011807c4ed442d98219e877a12207f4",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"EditGrid(children=(HBox(children=(HTML(value=''), HTML(value=''))), VBox(children=(CrudButtonBar(children=(Tog…"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from ipyautoui.custom.editgrid import EditGrid\n",
"class TestDataFrame(RootModel):\n",
" \"\"\"a description of TestDataFrame\"\"\"\n",
"\n",
" root: ty.List[DataFrameCols] = Field(format=\"dataframe\")\n",
"egrid = EditGrid(schema=TestDataFrame)\n",
"egrid"
]
},
{
"cell_type": "code",
"execution_count": 11,
"id": "58910e94-7696-42dc-b34f-567ffe3d9846",
"metadata": {},
"outputs": [],
"source": [
"egrid.value = [\n",
" {\n",
" \"string\": \"important string\",\n",
" \"integer\": 1,\n",
" \"floater\": 3.14,\n",
" \"something_else\": 324,\n",
" },\n",
" {\"string\": \"update\", \"integer\": 4, \"floater\": 3.12344, \"something_else\": 123},\n",
" {\"string\": \"evening\", \"integer\": 5, \"floater\": 3.14, \"something_else\": 235},\n",
" {\"string\": \"morning\", \"integer\": 5, \"floater\": 3.14, \"something_else\": 12},\n",
" {\"string\": \"number\", \"integer\": 3, \"floater\": 3.14, \"something_else\": 123},\n",
" ]"
]
},
{
"cell_type": "code",
"execution_count": 12,
"id": "6f11050a-db77-419a-b4d3-6e590ec4d62e",
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "5056a077bd6d45ccaf96588b0151f05f",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"EditGrid(children=(HBox(children=(HTML(value='The Wonderful Edit Grid Application'), HTML(value='Use…"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"from pydantic import RootModel\n",
"\n",
"# Test: EditGrid instance with multi-indexing.\n",
"AUTO_GRID_DEFAULT_VALUE = [\n",
" {\n",
" \"string\": \"important string\",\n",
" \"integer\": 1,\n",
" \"floater\": 3.14,\n",
" },\n",
"]\n",
"AUTO_GRID_DEFAULT_VALUE = AUTO_GRID_DEFAULT_VALUE * 4\n",
"AUTO_GRID_DEFAULT_VALUE = AUTO_GRID_DEFAULT_VALUE + [\n",
" {\n",
" \"string\": None,\n",
" \"integer\": None,\n",
" \"floater\": None,\n",
" },\n",
"]\n",
"\n",
"class DataFrameCols(BaseModel):\n",
" string: ty.Optional[str] = Field(\n",
" \"string\", json_schema_extra=dict(column_width=400, section=\"a\")\n",
" )\n",
" integer: ty.Optional[int] = Field(\n",
" 1, json_schema_extra=dict(column_width=80, section=\"a\")\n",
" )\n",
" floater: ty.Optional[float] = Field(\n",
" None, json_schema_extra=dict(column_width=70, section=\"b\")\n",
" )\n",
"\n",
"class TestDataFrame(RootModel):\n",
" \"\"\"a description of TestDataFrame\"\"\"\n",
"\n",
" root: ty.List[DataFrameCols] = Field(\n",
" default=AUTO_GRID_DEFAULT_VALUE,\n",
" json_schema_extra=dict(\n",
" format=\"dataframe\", datagrid_index_name=(\"section\", \"title\")\n",
" ),\n",
" )\n",
"\n",
"title = \"The Wonderful Edit Grid Application\"\n",
"description = \"Useful for all editing purposes whatever they may be 👍\"\n",
"editgrid = EditGrid(\n",
" schema=TestDataFrame,\n",
" title=title,\n",
" description=description,\n",
" ui_add=None,\n",
" ui_edit=None,\n",
" warn_on_delete=True,\n",
" show_copy_dialogue=False,\n",
" close_crud_dialogue_on_action=False,\n",
" global_decimal_places=1,\n",
" column_width={\"String\": 400},\n",
")\n",
"editgrid.observe(lambda c: print(\"_value changed\"), \"_value\")\n",
"display(editgrid)"
]
},
{
"cell_type": "code",
"execution_count": 16,
"id": "e2f1fd8e-0099-4a17-a8e9-545b2be1db67",
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "2a5a4f25cca849f98e3b7de81693ee02",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"HTML(value='None')"
]
},
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import ipywidgets as w\n",
"w.HTML(\"None\")"
]
},
{
"cell_type": "code",
"execution_count": 22,
"id": "303d4de2-5c58-4262-ada0-41c5b7958785",
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "2471d1f41a1241aba0a02f7144c267b7",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"EditGrid(children=(HBox(children=(HTML(value=''), HTML(value=''))), VBox(children=(CrudButtonBar(children=(Tog…"
]
},
"execution_count": 22,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"class TestProperties(BaseModel):\n",
" string: str\n",
" nullable_string: ty.Optional[str] = None\n",
" floater: float = 1.5\n",
" inty: int = 1\n",
"\n",
"\n",
"class TestGridSchema(RootModel):\n",
" \"\"\"no default\"\"\"\n",
"\n",
" root: ty.List[TestProperties] = Field(\n",
" [TestProperties(string=\"string\").model_dump()],\n",
" )\n",
"\n",
"egrid = EditGrid(\n",
" schema=TestGridSchema,\n",
" value=[{\"string\": \"test2\",\"nullable_string\":None, \"floater\": 2.2, \"inty\": 1}],\n",
" )\n",
"egrid"
]
},
{
"cell_type": "code",
"execution_count": 19,
"id": "85032563-8cf4-4b0d-93c6-aa2cd875e7cd",
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "4eb47f03dcca451ba3559b98dafc14eb",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"AutoObjectForm(children=(HBox(children=(ToggleButton(value=False, icon='window-maximize', layout=Layout(width=…"
]
},
"execution_count": 19,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from ipyautoui.autoobject import AutoObjectForm\n",
"from ipyautoui.demo_schemas import CoreIpywidgets\n",
"ui = AutoObjectForm.from_pydantic_model(CoreIpywidgets)\n",
"ui"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "3426e3da-306c-4a28-a848-a86fe7aa06e0",
"metadata": {},
"outputs": [],
"source": [
"# Row Validation (WIP)\n",
"\n",
"from pydantic import RootModel\n",
"\n",
"\n",
"class Sub(BaseModel):\n",
" a: str = \"a\"\n",
" b: int = 1\n",
"\n",
"\n",
"class Table(RootModel):\n",
" root: list[Sub]\n",
"\n",
"\n",
"class NestedTable(BaseModel):\n",
" table: list[Sub]\n",
"\n",
"\n",
"# for simple root tables it is simple to extract the pydantic model for the row\n",
"RowSchema = Table.__pydantic_core_schema__[\"schema\"][\"items_schema\"][\"cls\"]\n",
"print(RowSchema)\n",
"\n",
"# more difficult for nested rows\n",
"NestedRowSchema = NestedTable.__pydantic_core_schema__[\"schema\"][\"fields\"][\"table\"][\n",
" \"schema\"\n",
"][\"items_schema\"][\"cls\"]\n",
"print(NestedRowSchema)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "6e3efbd4-602c-42ee-95ae-72e07cb25b17",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.0"
}
},
"nbformat": 4,
"nbformat_minor": 5
}