{"id":20482322,"url":"https://github.com/agoose77/traitlet_widgets","last_synced_at":"2025-04-13T14:24:38.229Z","repository":{"id":62585118,"uuid":"197057492","full_name":"agoose77/traitlet_widgets","owner":"agoose77","description":"Widget generation from a traitlets HasTraits model","archived":false,"fork":false,"pushed_at":"2022-09-27T15:04:28.000Z","size":145,"stargazers_count":4,"open_issues_count":1,"forks_count":1,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-04-11T08:17:19.372Z","etag":null,"topics":["ipywidgets","jupyter","mvc","notebook","traitlets"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/agoose77.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-07-15T19:12:14.000Z","updated_at":"2022-10-09T11:08:20.000Z","dependencies_parsed_at":"2022-11-03T22:03:25.714Z","dependency_job_id":null,"html_url":"https://github.com/agoose77/traitlet_widgets","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/agoose77%2Ftraitlet_widgets","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/agoose77%2Ftraitlet_widgets/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/agoose77%2Ftraitlet_widgets/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/agoose77%2Ftraitlet_widgets/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/agoose77","download_url":"https://codeload.github.com/agoose77/traitlet_widgets/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248726065,"owners_count":21151845,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["ipywidgets","jupyter","mvc","notebook","traitlets"],"created_at":"2024-11-15T16:12:30.166Z","updated_at":"2025-04-13T14:24:38.204Z","avatar_url":"https://github.com/agoose77.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Traitlet Widgets\n[![pypi-badge][]][pypi] \n\n[pypi-badge]: https://img.shields.io/pypi/v/traitlet-widgets\n[pypi]: https://pypi.org/project/traitlet-widgets\n\nExamples\n--------\n## Model View\nFor a given model:\n```python\nfrom traitlets import HasTraits, Integer, Unicode\n\nclass Model(HasTraits):\n    age = Integer(min=0)\n    name = Unicode()\n```\n\nWe can create a view using `model_view`\n```python\nfrom traitlet_widgets import model_view\n\nmodel = Model()\nview = model_view(model)\n```\nwhich gives  \n![Screenshot of result of `model_view`](images/model_view.png)\n\n## UI Customisation\nThe UI can be customised through the use of `TraitType.tag()` or a custom transformer function.\n\n### Metadata\nAny tag set on a trait will be trialed as a widget attribute. If the attribute does not exist on the widget then it will not be set.\nFor example:\n```python\nfrom traitlets import HasTraits, Integer, Unicode\nfrom traitlet_widgets import model_view\n\nclass Model(HasTraits):\n    age = Integer(min=0)\n    name = Unicode().tag(description=\"A girl has no name\")\n\nmodel = Model()\n\nmodel_view(model)\n```\n\n![Screenshot of result of `model_view`](images/model_view_tag.png)\n\nThe same metadata that can be provided using tags can also be provided through a\n hierarchical metadata dictionary:\n```python\nmodel_view(model, metadata={\"name\": {\"description\": \"Model name meta\"}})\n```\nThis may be desired if it is important to hide UI details from the model.\n \n### Variants\n\nIt is possible to override the variant used for a particular trait, provided it is compatible with the underlying trait:\n```python\nfrom traitlets import HasTraits, Integer, Unicode\nfrom traitlet_widgets import  model_view\n\nclass Model(HasTraits):\n    age = Integer(min=0, max=10)\n    name = Unicode()\n\nmodel_view(Model())\n```\n![Screenshot of result of `model_view`](images/model_view_slider.png)\n\nLet's create a bounded integer text view for the age field:\n```python\nfrom traitlets import HasTraits, Integer, Unicode\nfrom traitlet_widgets import  model_view\nimport ipywidgets as widgets\n\nclass Model(HasTraits):\n    age = Integer(min=0, max=10).tag(variant=widgets.BoundedIntText)\n    name = Unicode()\n\nmodel_view(Model())\n```\n![Screenshot of result of `model_view`](images/model_view_bounded.png)\n\n\n### Advanced Usage\nTo further control how a view is generated from a model, you can subclass the view\nfactory. For example, given this model\n```python\nfrom traitlets import HasTraits, Integer, Unicode\nfrom traitlet_widgets import  model_view\nimport ipywidgets as widgets\n\nclass Model(HasTraits):\n    age = Integer(min=0, max=10).tag(variant=widgets.BoundedIntText)\n    name = Unicode()\n\nmodel_view(Model())\n```\nwe can create a custom view factory:\n```python\nfrom traitlet_widgets import ViewFactory\n\nclass CustomFactory(ViewFactory):\n\n    def create_trait_view(self, trait, ctx):\n        default_widget = super().create_trait_view(trait, ctx)\n\n        if ctx.name == \"name\":\n            default_widget.description = \"A girl has no name\"\n            default_widget.style = {\"description_width\": \"initial\"}\n\n        return default_widget\n\nf = CustomFactory()\nf.create_root_view(Model())\n```\n\n![Screenshot of result of `model_view`](images/model_view_tag.png)\n\n### Adding custom widgets\nWith the `trait_view_variants` decorator, it is possible to register custom view\nvariants for a particular trait type, for example:\n```python\nfrom traitlet_widgets import trait_view_variants\nimport ipywidgets as widgets\nimport traitlets\n\n@trait_view_variants(traitlets.Bool)\ndef bool_view_factory(trait, ctx):\n    yield widgets.ToggleButton, ctx.metadata\n    ...\n```\n\nBut sometimes you might wish to forgo the metadata-driven UI, and directly\nspecify the factory and its arguments. For this, you can use the `factory` metadata\nfield:\n```python\nfrom traitlets import HasTraits, Integer, Unicode\nfrom traitlet_widgets import model_view\nimport ipywidgets as widgets\n\n\nclass Model(HasTraits):\n    age = Integer(min=0, max=10)\n    name = Unicode()\n\n\ndef play_factory(trait, ctx):\n    return widgets.Play, {\n        \"min\": trait.get(\"min\", 0),\n        \"max\": trait.get(\"max\", 100),\n        **ctx.metadata,\n    }\n\n\nmodel_view(Model(), metadata={\"age\": {\"factory\": play_factory}})\n\n```\n\n### Further Customisation\nAlthough this library is driven by metadata, it is possible to modify this\nbehaviour by overriding the appropriate methods on the view factory.\nThese methods govern the trait discovery, view creation, and trait filtering. \n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fagoose77%2Ftraitlet_widgets","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fagoose77%2Ftraitlet_widgets","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fagoose77%2Ftraitlet_widgets/lists"}