Skip to content

Commit 8ab3a4c

Browse files
jtpiowolfv
authored andcommitted
Add Dashboard Builder section in DnD notebook
1 parent 0fac96c commit 8ab3a4c

File tree

2 files changed

+162
-0
lines changed

2 files changed

+162
-0
lines changed

docs/source/examples/Drag and Drop.ipynb

Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -539,6 +539,168 @@
539539
" fig_box,\n",
540540
" fig2_box])"
541541
]
542+
},
543+
{
544+
"cell_type": "markdown",
545+
"metadata": {},
546+
"source": [
547+
"## Dashboard Builder\n",
548+
"\n",
549+
"The drag and drop widgets can also be used to build a dashboard interactively.\n",
550+
"\n",
551+
"First let's define the structure of the dashboard by using the `AppLayout` layout template widget."
552+
]
553+
},
554+
{
555+
"cell_type": "code",
556+
"execution_count": null,
557+
"metadata": {},
558+
"outputs": [],
559+
"source": [
560+
"from ipywidgets import AppLayout, Button, DropBox, Layout, VBox"
561+
]
562+
},
563+
{
564+
"cell_type": "markdown",
565+
"metadata": {},
566+
"source": [
567+
"Whenever a `DraggableBox` widget is dropped in a `Dropbox`, the content of the `Dropbox` will be replaced by the widget."
568+
]
569+
},
570+
{
571+
"cell_type": "code",
572+
"execution_count": null,
573+
"metadata": {},
574+
"outputs": [],
575+
"source": [
576+
"def attach_to_box(box, widget):\n",
577+
" box.child = widget\n",
578+
"\n",
579+
"\n",
580+
"def create_expanded_dropbox(button_style):\n",
581+
" box = DropBox(Button(description='Drop widget here', button_style=button_style, layout=Layout(width='100%', height='100%')))\n",
582+
" box.on_drop(lambda *args: attach_to_box(box, args[1]['widget']))\n",
583+
" return box"
584+
]
585+
},
586+
{
587+
"cell_type": "markdown",
588+
"metadata": {},
589+
"source": [
590+
"Let's create the app layout:"
591+
]
592+
},
593+
{
594+
"cell_type": "code",
595+
"execution_count": null,
596+
"metadata": {},
597+
"outputs": [],
598+
"source": [
599+
"header = create_expanded_dropbox('success')\n",
600+
"left_sidebar = create_expanded_dropbox('info')\n",
601+
"center = create_expanded_dropbox('warning')\n",
602+
"right_sidebar = create_expanded_dropbox('info')\n",
603+
"footer = create_expanded_dropbox('success')"
604+
]
605+
},
606+
{
607+
"cell_type": "code",
608+
"execution_count": null,
609+
"metadata": {},
610+
"outputs": [],
611+
"source": [
612+
"app = AppLayout(\n",
613+
" header=header,\n",
614+
" left_sidebar=left_sidebar,\n",
615+
" center=center,\n",
616+
" right_sidebar=right_sidebar,\n",
617+
" footer=footer\n",
618+
")\n",
619+
"app"
620+
]
621+
},
622+
{
623+
"cell_type": "markdown",
624+
"metadata": {},
625+
"source": [
626+
"Now let's create the widgets that will be part of the dashboard."
627+
]
628+
},
629+
{
630+
"cell_type": "code",
631+
"execution_count": null,
632+
"metadata": {},
633+
"outputs": [],
634+
"source": [
635+
"from ipywidgets import DraggableBox, Dropdown, IntProgress, IntSlider, Label, Tab, Text, link"
636+
]
637+
},
638+
{
639+
"cell_type": "code",
640+
"execution_count": null,
641+
"metadata": {},
642+
"outputs": [],
643+
"source": [
644+
"title = Label('My Custom Dashboard', layout=Layout(display='flex', justify_content='center', width='auto'))\n",
645+
"DraggableBox(title, draggable=True)"
646+
]
647+
},
648+
{
649+
"cell_type": "code",
650+
"execution_count": null,
651+
"metadata": {},
652+
"outputs": [],
653+
"source": [
654+
"slider = IntSlider(min=0, max=10, step=1, layout=Layout(width='auto'), description='Slider')\n",
655+
"DraggableBox(slider, draggable=True)"
656+
]
657+
},
658+
{
659+
"cell_type": "code",
660+
"execution_count": null,
661+
"metadata": {},
662+
"outputs": [],
663+
"source": [
664+
"progress = IntProgress(\n",
665+
" min=0,\n",
666+
" max=10,\n",
667+
" step=1,\n",
668+
" description='Loading:',\n",
669+
" orientation='horizontal',\n",
670+
" layout=Layout(width='auto')\n",
671+
")\n",
672+
"link((slider, 'value'), (progress, 'value'))\n",
673+
"DraggableBox(progress, draggable=True)"
674+
]
675+
},
676+
{
677+
"cell_type": "code",
678+
"execution_count": null,
679+
"metadata": {},
680+
"outputs": [],
681+
"source": [
682+
"tab_contents = ['P0', 'P1', 'P2', 'P3', 'P4']\n",
683+
"children = [Text(description=name) for name in tab_contents]\n",
684+
"tab = Tab()\n",
685+
"tab.children = children\n",
686+
"for i in range(len(children)):\n",
687+
" tab.set_title(i, str(i))\n",
688+
"\n",
689+
"DraggableBox(tab, draggable=True)"
690+
]
691+
},
692+
{
693+
"cell_type": "markdown",
694+
"metadata": {},
695+
"source": [
696+
"Now let's drag the widgets and drop them in the app layout!\n",
697+
"\n",
698+
"In JupyterLab you can open the widget in a different panel by right clicking on the `AppLayout` widget and selecting `Create New View for Output`:\n",
699+
"\n",
700+
"![create-new-view-for-output](./images/create-new-view-for-output.png)\n",
701+
"\n",
702+
"This makes dragging widgets to the app layout more convenient."
703+
]
542704
}
543705
],
544706
"metadata": {
Loading

0 commit comments

Comments
 (0)