{ "cells": [ { "cell_type": "markdown", "id": "09d8dac5", "metadata": {}, "source": [ "OO-LD UI Demo - [Open this notebook in JupyterLite](https://repolab.github.io/jupyterlite-playground/lab/index.html?fromURL=https://raw.githubusercontent.com/OO-LD/oold-python/refs/heads/main/examples/linked_data_editor.ipynb)" ] }, { "cell_type": "markdown", "id": "cfd8cd86", "metadata": {}, "source": [ "Install required packages. In a pyodide/jupyterlite environment this may take ~30 seconds" ] }, { "cell_type": "code", "execution_count": null, "id": "ac06ff07", "metadata": {}, "outputs": [], "source": [ "import sys\n", "if sys.platform == 'emscripten':\n", " # see: https://github.com/pyodide/pyodide/issues/5834\n", " import micropip\n", " micropip.uninstall(['typing-extensions'])\n", "%pip install bokeh==3.7.3 jupyter_bokeh panel==1.7.5 oold[UI-panel]\n", "# in jupyterlite this is equivalent to:\n", "# import piplite\n", "# await piplite.install([bokeh==3.7.3', 'jupyter_bokeh', 'panel==1.7.5', 'oold[UI-panel]'])" ] }, { "cell_type": "markdown", "id": "9fc3d71d", "metadata": {}, "source": [ "Define a linked data model and spin up editor UI\n", "\n", "*Note: In a actual implementation you may import data models from a registry like https://github.com/OpenSemanticWorld-Packages*" ] }, { "cell_type": "code", "execution_count": null, "id": "ffdbdabe", "metadata": {}, "outputs": [], "source": [ "from enum import Enum\n", "from typing import List, Optional\n", "\n", "import panel as pn\n", "from pydantic import ConfigDict, Field\n", "\n", "from oold.model import LinkedBaseModel\n", "from oold.ui.panel.demo import OoldDemoEditor\n", "\n", "\n", "class Entity(LinkedBaseModel):\n", " model_config = ConfigDict(\n", " json_schema_extra={\n", " \"@context\": {\n", " # aliases\n", " \"id\": \"@id\",\n", " \"type\": \"@type\",\n", " # prefixes\n", " \"schema\": \"https://schema.org/\",\n", " \"ex\": \"https://example.com/\",\n", " # literal property\n", " \"name\": \"schema:name\",\n", " },\n", " \"iri\": \"Entity.json\", # the IRI of the schema\n", " }\n", " )\n", " type: Optional[str] = Field(\n", " \"ex:Entity.json\",\n", " json_schema_extra={\"options\": {\"hidden\": \"true\"}},\n", " )\n", " name: str\n", "\n", " def get_iri(self):\n", " return \"ex:\" + self.name\n", "\n", "\n", "class Hobby(str, Enum):\n", " \"\"\"Various hobbies as an enum.\"\"\"\n", "\n", " SPORTS = \"ex:sports\"\n", " \"\"\"Sports hobby, e.g. football, basketball, etc.\"\"\"\n", " MUSIC = \"ex:music\"\n", " \"\"\"Music hobby, e.g. playing instruments, singing, etc.\"\"\"\n", " ART = \"ex:art\"\n", " \"\"\"Art hobby, e.g. painting, drawing, etc.\"\"\"\n", "\n", "\n", "class Person(Entity):\n", " \"\"\"A simple Person schema\"\"\"\n", "\n", " model_config = ConfigDict(\n", " json_schema_extra={\n", " \"@context\": [\n", " \"Entity.json\", # import the context of the parent class\n", " {\n", " # object property definition\n", " \"hobbies\": {\n", " \"@id\": \"ex:hobbies\",\n", " \"@type\": \"@id\",\n", " },\n", " \"knows\": {\n", " \"@id\": \"schema:knows\",\n", " \"@type\": \"@id\",\n", " \"@container\": \"@set\",\n", " },\n", " },\n", " ],\n", " \"iri\": \"Person.json\",\n", " \"defaultProperties\": [\"type\", \"name\", \"hobbies\"],\n", " }\n", " )\n", " type: Optional[str] = \"ex:Person.json\"\n", " hobbies: Optional[List[Hobby]] = None\n", " \"\"\"interests of the person, e.g. sports, music, art\"\"\"\n", " knows: Optional[List[\"Person\"]] = Field(\n", " None,\n", " # object property pointing to another Person\n", " json_schema_extra={\"range\": \"Person.json\"},\n", " )\n", "\n", "\n", "editor = OoldDemoEditor(Person)\n", "editor.servable()" ] }, { "cell_type": "code", "execution_count": null, "id": "91797cd7-ff9b-438e-8654-725a917df397", "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.11.6" } }, "nbformat": 4, "nbformat_minor": 5 }