{ "cells": [ { "cell_type": "markdown", "id": "45387a7d-960c-43a8-97cb-16e84d4af375", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "# `jupyterlite-pyodide-lock`\n", "\n", "Many features of the `jupyterlite-pyodide-lock` can be configured along with\n", "other JupyterLite features in your `jupyter_lite_config.json`." ] }, { "cell_type": "code", "execution_count": null, "id": "984659f3-83c0-4405-bbc8-62fdd5e32c86", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [ "remove-cell" ] }, "outputs": [], "source": [ "import json\n", "\n", "import traitlets\n", "from IPython.display import HTML\n", "from jupyterlite_core.trait_types import TypedTuple\n", "from nbconvert.filters.markdown_mistune import markdown2html_mistune\n", "from traitlets.config import Configurable" ] }, { "cell_type": "code", "execution_count": null, "id": "380bfe30-bb71-498b-a28c-d0bf972f2558", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [ "remove-cell" ] }, "outputs": [], "source": [ "TArrayish = traitlets.List | traitlets.Tuple | TypedTuple\n", "TNumberish = traitlets.CInt | traitlets.Int | traitlets.Float" ] }, { "cell_type": "code", "execution_count": null, "id": "f7335f8b-b7b7-4c6f-b3b1-b9b62675e1fb", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [ "remove-cell" ] }, "outputs": [], "source": [ "def markdown(md: str) -> HTML:\n", " \"\"\"Generate some HTML.\"\"\"\n", " return HTML(markdown2html_mistune(md))" ] }, { "cell_type": "code", "execution_count": null, "id": "399e2d29-a206-40de-b61f-e6397d3b01ec", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [ "remove-cell" ] }, "outputs": [], "source": [ "def trait_to_json_type(trait: traitlets.TraitType) -> list[str]:\n", " \"\"\"Extract a simplified JSON type from a trait.\"\"\"\n", " json_type = \"\"\n", " item_type = \"\"\n", " if isinstance(trait, TArrayish):\n", " json_type = \"array\"\n", " item_type = trait_to_json_type(trait._trait)[0] # noqa: SLF001\n", " elif isinstance(trait, traitlets.Enum):\n", " return \"string\", \"
\".join(f\"`{v}`\" for v in trait.values) # noqa: PD011\n", " elif isinstance(trait, traitlets.Bool):\n", " json_type = \"boolean\"\n", " elif isinstance(trait, traitlets.Unicode):\n", " json_type = \"string\"\n", " elif isinstance(trait, TNumberish):\n", " json_type = \"number\"\n", " elif isinstance(trait, traitlets.Dict):\n", " json_type = \"object\"\n", " else:\n", " msg = f\"unexpected trait {trait}\"\n", " raise ValueError(msg)\n", " return json_type, item_type" ] }, { "cell_type": "code", "execution_count": null, "id": "49a76362-02a7-4804-a21a-1c578ea839ba", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [ "remove-cell" ] }, "outputs": [], "source": [ "def trait_to_json_type_and_default(trait: traitlets.TraitType) -> list[str]:\n", " \"\"\"Extract a simplified JSON type and default from a trait.\"\"\"\n", " json_type, item_type = trait_to_json_type(trait)\n", "\n", " default_value = trait.default_value\n", "\n", " if isinstance(trait, traitlets.Dict):\n", " default_value = {}\n", " elif trait.default_value is traitlets.Undefined:\n", " default_value = None\n", " if isinstance(trait, TArrayish):\n", " default_value = []\n", " return [\n", " json_type if json_type else \"\",\n", " item_type if item_type else \"\",\n", " (\"\" if default_value is None else f\"`{json.dumps(default_value)}`\"),\n", " ]" ] }, { "cell_type": "code", "execution_count": null, "id": "5d07f2a9-0e84-4942-a6b5-89ba7bacddcc", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [ "remove-cell" ] }, "outputs": [], "source": [ "def config_table(importable: str) -> str:\n", " \"\"\"Generate a directive for a filtered configurable.\"\"\"\n", " current = __import__(importable.rsplit(\".\", 1)[0])\n", " for bit in importable.split(\".\")[1:]:\n", " current = getattr(current, bit)\n", " if not issubclass(current, Configurable):\n", " msg = f\"{importable} is not a Configurable\"\n", " raise ValueError(msg)\n", " lines = [\n", " f\"### {current.__name__}\",\n", " \"| name | help | type | [items] | default |\",\n", " \"|:-|:-|:-:|:-:|:-|\",\n", " ]\n", " traits = current._traits # noqa: SLF001\n", " configurables = {\n", " trait_name: trait\n", " for trait_name, trait in sorted(traits.items())\n", " if trait.metadata.get(\"config\")\n", " }\n", " for trait_name, trait in configurables.items():\n", " line = [\n", " f\"`{trait_name}`\",\n", " f\"{trait.help}\",\n", " *trait_to_json_type_and_default(trait),\n", " ]\n", " lines += [f\"\"\"| {\" | \".join(line)} |\"\"\"]\n", " return \"\\n\".join(lines)" ] }, { "cell_type": "markdown", "id": "f205b995-eb0d-4ae7-9215-160075bc704f", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "## Addons\n", "\n", "The `PyodideLockAddon` provides the main tasks for working with lockfile requirements." ] }, { "cell_type": "code", "execution_count": null, "id": "ab16cc73-a4ef-484d-ab70-529b124b08cc", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [ "remove-input" ] }, "outputs": [], "source": [ "if __name__ == \"__main__\":\n", " display(\n", " markdown(config_table(\"jupyterlite_pyodide_lock.addons.lock.PyodideLockAddon\"))\n", " )" ] }, { "cell_type": "markdown", "id": "1cee7c1e-0656-4940-a5e0-508906a90128", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "## Lockers\n", "\n", "The `BaseLocker` subclasses provide an WebAssembly environment in which to run `micropip.freeze`." ] }, { "cell_type": "code", "execution_count": null, "id": "98237d4f-bf25-43ea-a0bd-9fc86ab32e06", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [ "remove-input" ] }, "outputs": [], "source": [ "if __name__ == \"__main__\":\n", " display(\n", " markdown(config_table(\"jupyterlite_pyodide_lock.lockers.browser.BrowserLocker\"))\n", " )" ] } ], "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.3" } }, "nbformat": 4, "nbformat_minor": 5 }