{ "cells": [ { "cell_type": "markdown", "id": "0", "metadata": {}, "source": [ "
You are looking at an HTML rendering of the generated widgets, they are lacking responsiveness due to no Python kernel being available for callbacks.
" ] }, { "cell_type": "markdown", "id": "1", "metadata": {}, "source": [ "# Form from Pydantic models" ] }, { "cell_type": "markdown", "id": "2", "metadata": {}, "source": [ "Pass a Pydantic model class directly. The schema is derived automatically." ] }, { "cell_type": "code", "execution_count": null, "id": "3", "metadata": {}, "outputs": [], "source": [ "from pydantic import BaseModel, Field\n", "from ipywidgets_jsonschema import Form\n", "\n", "\n", "class InstrumentConfig(BaseModel):\n", " instrument_name: str = Field(\"spectrometer\", description=\"Display name\")\n", " serial_number: str = Field(\"ABC-0001\", pattern=r\"^[A-Z]{3}-[0-9]{4}$\")\n", " sample_rate_hz: int = Field(1000, ge=1, le=5000)\n", " calibration_gain: float = Field(1.0, ge=0.1, le=10.0)\n", " enabled: bool = True\n", "\n", "\n", "pydantic_form = Form(InstrumentConfig, show_descriptions=True)\n", "pydantic_form.show()" ] }, { "cell_type": "code", "execution_count": null, "id": "4", "metadata": {}, "outputs": [], "source": [ "pydantic_form.data" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "name": "python", "version": "3" } }, "nbformat": 4, "nbformat_minor": 5 }