Skip to content

Commit

Permalink
Add helper
Browse files Browse the repository at this point in the history
  • Loading branch information
edan-bainglass committed Nov 21, 2024
1 parent d623c74 commit 91def19
Showing 1 changed file with 52 additions and 17 deletions.
69 changes: 52 additions & 17 deletions bands_plot_poc.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"cells": [
{
"cell_type": "code",
"execution_count": null,
"execution_count": 11,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -12,41 +12,45 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 12,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"import plotly.graph_objects as go\n",
"\n",
"\n",
"class Model(tl.HasTraits):\n",
" plot = tl.Instance(go.FigureWidget, allow_none=True)\n",
"class Helper:\n",
" def init(self):\n",
" self.plot = go.FigureWidget()\n",
" self.plot.add_scatter(y=[2, 1, 4, 3])\n",
"\n",
"\n",
"class Model(tl.HasTraits):\n",
" seed = tl.Int(0)\n",
"\n",
" def __init__(self, *args, **kwargs):\n",
" super().__init__(*args, **kwargs)\n",
" self.plot = go.FigureWidget()\n",
"\n",
" self._init_plot()\n",
" self._helper = Helper()\n",
" self._helper.init()\n",
"\n",
" def _init_plot(self):\n",
" self.plot.add_scatter(y=[2, 1, 4, 3])\n",
" @property\n",
" def plot(self):\n",
" return self._helper.plot\n",
"\n",
" def update_plot(self):\n",
" np.random.seed(self.seed)\n",
"\n",
" self.plot.data[0].y = np.random.rand(len(self.plot.data[0].y))"
" self._helper.plot.data[0].y = np.random.rand(len(self.plot.data[0].y))"
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 13,
"metadata": {},
"outputs": [],
"source": [
"class View(ipw.VBox):\n",
"class ViewController(ipw.VBox):\n",
" \"\"\"\"\"\"\n",
"\n",
" def __init__(self, model: Model, **kwargs):\n",
Expand All @@ -72,22 +76,45 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 18,
"metadata": {},
"outputs": [],
"source": [
"model = Model()\n",
"view = View(model)"
"vc = ViewController(model)"
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 19,
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "ac3102bd7834466a9aa996c7b15a4b8d",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"ViewController(children=(FigureWidget({\n",
" 'data': [{'type': 'scatter', 'uid': '8db32a44-ee27-4e6a-9923-c2d62…"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"view"
"vc"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
Expand All @@ -97,7 +124,15 @@
"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.9.13"
}
},
Expand Down

0 comments on commit 91def19

Please sign in to comment.