Skip to content

Commit

Permalink
colab support
Browse files Browse the repository at this point in the history
added colab suport to info widget
ipyWidgets.tab is not working
  • Loading branch information
gduscher committed Nov 23, 2024
1 parent a2334a7 commit 27c6df1
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 27 deletions.
54 changes: 48 additions & 6 deletions notebooks/Spectroscopy/Analyse_Low_Loss.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -202,12 +202,12 @@
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "c74560c16a04411fa7ed276ff0b7cbd0",
"model_id": "14d4d0a343d241c7ac7e9363ae2121a5",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"AppLayout(children=(Tab(children=(GridspecLayout(children=(Dropdown(description='directory:', layout=Layout(gr…"
"AppLayout(children=(VBox(children=(ToggleButtons(layout=Layout(width='auto'), options=('File', 'Spec.', 'LowLo…"
]
},
"metadata": {},
Expand All @@ -224,19 +224,59 @@
},
{
"cell_type": "code",
"execution_count": 15,
"execution_count": 8,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"4083964757.425642\n"
"Channel_000 Channel_001 0\n",
"Channel_000 Channel_001 0\n",
"Channel_001 Channel_001 0\n",
"Channel_001 Channel_001 1\n",
"resolution_function Channel_001 1\n",
"resolution_function Channel_001 1\n",
"plasmon Channel_001 1\n",
"plasmon Channel_001 1\n",
"multiple_scattering Channel_001 1\n",
"multiple_scattering Channel_001 1\n"
]
}
],
"source": [
"infoWidget.low_loss.get_drude()\n"
"infoWidget.tab_buttons.index = 2\n",
"\n",
"infoWidget.low_loss.update_ll_sidebar()\n"
]
},
{
"cell_type": "code",
"execution_count": 121,
"metadata": {},
"outputs": [],
"source": [
"infoWidget.tabval = 2\n",
"infoWidget.tab_activated()"
]
},
{
"cell_type": "code",
"execution_count": 46,
"metadata": {},
"outputs": [],
"source": [
"import ipywidgets\n",
"infoWidget.tab.children = [infoWidget.tab_buttons, infoWidget.children[infoWidget.tab_buttons.index]]"
]
},
{
"cell_type": "code",
"execution_count": 56,
"metadata": {},
"outputs": [],
"source": [
"infoWidget.info.update_dataset()"
]
},
{
Expand Down Expand Up @@ -967,7 +1007,9 @@
},
{
"cell_type": "markdown",
"metadata": {},
"metadata": {
"jp-MarkdownHeadingCollapsed": true
},
"source": [
"## peakfit"
]
Expand Down
66 changes: 47 additions & 19 deletions pyTEMlib/info_widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import numpy as np
import os
import sys
import ipywidgets
import matplotlib.pylab as plt
import matplotlib
Expand Down Expand Up @@ -253,6 +254,11 @@ def __init__(self, datasets, sidebar, tab_title=None):
self.key = None
self.new_info = False
self.image = 'Sum'
if 'google.colab' in sys.modules:
self.google = True
else:
self.google = False
self.google = True

self.save_path = True

Expand All @@ -275,25 +281,40 @@ def __init__(self, datasets, sidebar, tab_title=None):
self.end_channel = -2

self.file_bar = get_file_widget_ui()
children = [self.file_bar]
titles = ['File']
if isinstance(sidebar, dict):
tab = ipywidgets.Tab()
children = [self.file_bar]
titles = ['File']

for sidebar_key, sidebar_gui in sidebar.items():
children.append(sidebar_gui)
titles.append(sidebar_key)
tab.children = children
tab.titles = titles
elif not isinstance(sidebar, list):
tab = ipywidgets.Tab()
tab.children = [self.file_bar, sidebar]
tab.titles = ['File', 'Info']
children = [self.file_bar, sidebar]
titles = ['File', 'Info']

if self.google:
self.buttons = []
for i in range(len(children)):
self.buttons.append(ipywidgets.Button(description=titles[i],
disabled=False,
button_style='', # 'success', 'info', 'warning', 'danger' or ''
layout=ipywidgets.Layout(width='800px')))


self.tab_buttons = ipywidgets.ToggleButtons(options=titles, description='', disabled=False,
layout=ipywidgets.Layout(width='auto'),
style={"button_width": "auto"})
tab = ipywidgets.VBox([self.tab_buttons, self.file_bar])
self.children = children

else:
tab = sidebar
self.tab = tab
tab = ipywidgets.Tab()
tab.children = children
tab.titles = titles

with plt.ioff():
self.figure = plt.figure()

self.tab =tab
self.figure.canvas.toolbar_position = 'right'
self.figure.canvas.toolbar_visible = True

Expand Down Expand Up @@ -739,18 +760,25 @@ def __init__(self, datasets=None):
self.set_action()

def set_action(self):
if self.google:
self.tab_buttons.observe(self.tab_activated)
self.tab.observe(self.tab_activated)

def tab_activated(self, val=0):
if isinstance(val.new, int):
self.tabval = val.new
if self.google:
self.tab.children = [self.tab_buttons, self.children[self.tab_buttons.index]]
self.tabval = self.tab_buttons.index
else:
if isinstance(val.new, int):
self.tabval = val.new
# self.update_sidebars()
if val.new == 1:
self.info.update_dataset()
elif val.new == 2:
self.low_loss.update_ll_sidebar()
elif val.new == 3:
self.core_loss.update_cl_sidebar()
if self.tabval == 1:
self.info.update_dataset()
elif self.tabval == 2:
self.low_loss.update_ll_sidebar()
elif self.tabval == 3:
self.core_loss.update_cl_sidebar()


def update_sidebars(self):
if hasattr(self, 'info'):
Expand Down
9 changes: 7 additions & 2 deletions pyTEMlib/low_loss_widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,9 +132,14 @@ def update_ll_sidebar(self):
if 'SPECTR' in self.parent.datasets[key].data_type.name:
energy_offset = self.parent.datasets[key].get_spectral_dims(return_axis=True)[0][0]
if energy_offset < 0:
spectrum_list.append(f'{key}: {self.parent.datasets[key].title}')
spectrum_list.append(f'{key}: {self.parent.datasets[key].title}')
print(key, self.ll_key, ll_index)
if key == self.ll_key:
ll_index = index+1
ll_index = index-1
print(key, self.ll_key, ll_index)
if ll_index >len(spectrum_list) - 1:
ll_index = len(spectrum_list) - 1

self.low_loss_tab[0, 0].options = spectrum_list
self.low_loss_tab[0, 0].value = spectrum_list[ll_index]

Expand Down

0 comments on commit 27c6df1

Please sign in to comment.