{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "7ba8c00d", "metadata": { "tags": [ "hide-input" ] }, "outputs": [], "source": [ "# %run ../src/ipyautoui/_dev_sys_path_append.py\n", "%run __init__.py\n", "%load_ext lab_black" ] }, { "cell_type": "markdown", "id": "ecc7e43a", "metadata": { "tags": [] }, "source": [ "# AutoUi" ] }, { "cell_type": "markdown", "id": "aa67ab0e", "metadata": {}, "source": [ "With ipyautoui we can create ipywidgets from either a **json-schema** or a **pydantic model**. This makes it quick and easy to whip up a user interface when required." ] }, { "cell_type": "code", "execution_count": 2, "id": "e13f11de-cb83-4a4b-9e90-026167ec098f", "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "b2b2422057ed4cb6afe251d1ea70b95c", "version_major": 2, "version_minor": 0 }, "text/plain": [ "DemoReel(children=(VBox(children=(HTML(value='⬇️ -- Select demo pydantic model / generated AutoUi -- ⬇️…" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "from ipyautoui import demo\n", "\n", "demo()" ] }, { "cell_type": "code", "execution_count": 3, "id": "c8630081", "metadata": {}, "outputs": [], "source": [ "from ipyautoui import AutoUi\n", "import json\n", "from pydantic import BaseModel, Field\n", "from ipyautoui.constants import DIR_MODULE\n", "from ipyautoui._utils import display_pydantic_json\n", "import ipyautoui\n", "import ipywidgets as w" ] }, { "cell_type": "code", "execution_count": 4, "id": "9d70dd9d-cd5b-4b59-a5d3-32a0f184c7bf", "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "7791de8fcd7d42e3b79496d9491634fb", "version_major": 2, "version_minor": 0 }, "text/plain": [ "AutoObject(children=(SaveButtonBar(children=(ToggleButton(value=False, button_style='success', disabled=True, …" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from pydantic import BaseModel, Field\n", "from ipyautoui.custom.fileupload import AutoUploadPaths\n", "from ipyautoui.autoipywidget import AutoObject\n", "\n", "class Test(BaseModel):\n", " paths: list[pathlib.Path] = Field(autoui=\"ipyautoui.custom.fileupload.AutoUploadPaths\")\n", " a: str\n", "\n", " class Config:\n", " schema_extra = {\n", " 'nested_widgets': ['ipyautoui.custom.fileupload.AutoUploadPaths']\n", " }\n", "aui = AutoObject(Test) # , nested_widgets=[AutoUploadPaths]\n", "aui" ] }, { "cell_type": "code", "execution_count": 5, "id": "cb49c754-8660-49e4-a65e-218f71fbe920", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'title': 'Test',\n", " 'type': 'object',\n", " 'properties': {'paths': {'title': 'Paths',\n", " 'autoui': 'ipyautoui.custom.fileupload.AutoUploadPaths',\n", " 'type': 'array',\n", " 'items': {'type': 'string', 'format': 'path'}},\n", " 'a': {'title': 'A', 'type': 'string'}},\n", " 'required': ['paths', 'a'],\n", " 'nested_widgets': ['ipyautoui.custom.fileupload.AutoUploadPaths']}" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "aui.show_nested()" ] }, { "cell_type": "markdown", "id": "21a44310", "metadata": {}, "source": [ "### Creating Simple Widget" ] }, { "cell_type": "code", "execution_count": 6, "id": "80057991", "metadata": {}, "outputs": [], "source": [ "from ipyautoui.test_schema import (\n", " TestAutoLogic,\n", " TestAutoLogicSimple,\n", ") # the schema shown in the file above" ] }, { "cell_type": "markdown", "id": "89a3984e", "metadata": {}, "source": [ "So let's create a simple pydantic class. Here we have one text field." ] }, { "cell_type": "code", "execution_count": 8, "id": "b0cb559e", "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "967ec45149f446758fe0df27fa890d7d", "version_major": 2, "version_minor": 0 }, "text/plain": [ "AutoUi(children=(SaveButtonBar(children=(ToggleButton(value=False, button_style='success', disabled=True, icon…" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "# create a pydantic model (or a json-schema) defining the fields of interest\n", "from pydantic import BaseModel, Field\n", "\n", "\n", "class AutoUiExample(BaseModel):\n", " text: str = Field(default=\"Test\", description=\"This description is very important\")\n", "\n", "\n", "data = {\"text\": \"this is a value\"}\n", "ui = AutoUi(schema=AutoUiExample, path=pathlib.Path(\"test.ui.json\"), show_savebuttonbar=False)\n", "display(ui)" ] }, { "cell_type": "code", "execution_count": 9, "id": "e1f990e7-ef47-48ca-9da3-c940a02ac5c1", "metadata": {}, "outputs": [], "source": [ "from ipyautoui.basemodel import file\n", "\n", "file(AutoUiExample(), pathlib.Path(\"test.json\"))" ] }, { "cell_type": "code", "execution_count": 10, "id": "4e4f84e4-a065-48bc-b1e9-a9a4633c86fd", "metadata": {}, "outputs": [], "source": [ "# \"\"\"extending default pydantic BaseModel. NOT IN USE.\"\"\"\n", "# import pathlib\n", "# from pydantic import BaseModel\n", "# from typing import Type\n", "\n", "\n", "# def file(self: Type[BaseModel], path: pathlib.Path, **json_kwargs):\n", "# \"\"\"\n", "# this is a method that is added to the pydantic BaseModel within AutoUi using\n", "# \"setattr\".\n", "\n", "# Example:\n", "# ```setattr(model, 'file', file)```\n", "\n", "# Args:\n", "# self (pydantic.BaseModel): instance\n", "# path (pathlib.Path): to write file to\n", "# \"\"\"\n", "# if \"indent\" not in json_kwargs.keys():\n", "# json_kwargs.update({\"indent\": 4})\n", "# path.write_text(self.json(**json_kwargs), encoding=\"utf-8\")\n", "\n", "\n", "# aui = AutoUiExample()\n", "# aui.__slots__ = ('file',)\n", "# setattr(aui, \"file\", file)\n", "\n", "\n", "# #object.__setattr__(aui, \"file\", file)\n", "# aui.file(pathlib.Path('test.json'))" ] }, { "cell_type": "markdown", "id": "9e97be5c", "metadata": { "tags": [] }, "source": [ "### Writing to JSON" ] }, { "cell_type": "markdown", "id": "c6d93fb4", "metadata": {}, "source": [ "Let's define the save location." ] }, { "cell_type": "code", "execution_count": 11, "id": "ce2e69e0", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Save Location is: test.simpleaui.json\n" ] } ], "source": [ "import pathlib\n", "\n", "save_path = pathlib.Path(\".\") / \"test.simpleaui.json\"\n", "print(f\"Save Location is: {save_path}\")" ] }, { "cell_type": "code", "execution_count": 12, "id": "b4c3a453", "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "4b8889d09e384db4b2de5b36b1838568", "version_major": 2, "version_minor": 0 }, "text/plain": [ "AutoRenderer(children=(SaveButtonBar(children=(ToggleButton(value=False, button_style='success', disabled=True…" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "ui.file(path=save_path)\n", "AutoUiRenderer = AutoUi.create_autoui_renderer(schema=AutoUiExample)\n", "\n", "ui_simple = AutoUiRenderer(path=save_path)\n", "\n", "\n", "def test_action():\n", " print(\"done\")\n", "\n", "\n", "ui_simple.savebuttonbar.fns_onsave_add_action(test_action)\n", "ui_simple.show_savebuttonbar = True\n", "display(ui_simple)" ] }, { "cell_type": "code", "execution_count": 13, "id": "9654668d-96f3-481c-b209-19e91b0316a8", "metadata": {}, "outputs": [], "source": [ "import ipywidgets as w\n", "\n", "# ui_simple.autowidget.insert_rows = {0: w.Button()}" ] }, { "cell_type": "code", "execution_count": 14, "id": "1d546f3e-1ce8-413e-96c5-50e3cfc3de49", "metadata": {}, "outputs": [], "source": [ "import typing as ty\n", "\n", "\n", "class DataFrameCols(BaseModel):\n", " string: str = Field(\"string\", aui_column_width=100)\n", " integer: int = Field(1, aui_column_width=80)\n", " floater: float = Field(3.1415, aui_column_width=70, global_decimal_places=3)\n", " something_else: float = Field(324, aui_column_width=100)\n", "\n", "\n", "class TestDataFrame(BaseModel):\n", " \"\"\"a description of TestDataFrame\"\"\"\n", "\n", " dataframe: ty.List[DataFrameCols] = Field(format=\"dataframe\")" ] }, { "cell_type": "code", "execution_count": 15, "id": "01ff66c3-36e3-42b5-a0b1-942991b84b35", "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "3c956f31cf074dd9ae3f83206a2fbde8", "version_major": 2, "version_minor": 0 }, "text/plain": [ "AutoUi(children=(SaveButtonBar(children=(ToggleButton(value=False, button_style='success', disabled=True, icon…" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "auto_grid = AutoUi(schema=TestDataFrame)\n", "display(auto_grid)" ] }, { "cell_type": "code", "execution_count": 14, "id": "213647db-a6ee-42e5-8afa-19a5336fe070", "metadata": {}, "outputs": [], "source": [ "auto_grid.value = {\n", " \"dataframe\": [\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", " ]\n", "}" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3.9 (XPython)", "language": "python", "name": "xpython" }, "language_info": { "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "version": "3.9.16" }, "vscode": { "interpreter": { "hash": "2945cf1448f5962318cd8d4564004b5e22175de685c8c5e6d8925611c08f83bc" } } }, "nbformat": 4, "nbformat_minor": 5 }