Skip to content

Commit 496417a

Browse files
authored
Merge pull request #179 from pycroscopy/use_dict3
colab support
2 parents 5d01a50 + 27c6df1 commit 496417a

File tree

3 files changed

+102
-27
lines changed

3 files changed

+102
-27
lines changed

notebooks/Spectroscopy/Analyse_Low_Loss.ipynb

Lines changed: 48 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -202,12 +202,12 @@
202202
{
203203
"data": {
204204
"application/vnd.jupyter.widget-view+json": {
205-
"model_id": "c74560c16a04411fa7ed276ff0b7cbd0",
205+
"model_id": "14d4d0a343d241c7ac7e9363ae2121a5",
206206
"version_major": 2,
207207
"version_minor": 0
208208
},
209209
"text/plain": [
210-
"AppLayout(children=(Tab(children=(GridspecLayout(children=(Dropdown(description='directory:', layout=Layout(gr…"
210+
"AppLayout(children=(VBox(children=(ToggleButtons(layout=Layout(width='auto'), options=('File', 'Spec.', 'LowLo…"
211211
]
212212
},
213213
"metadata": {},
@@ -224,19 +224,59 @@
224224
},
225225
{
226226
"cell_type": "code",
227-
"execution_count": 15,
227+
"execution_count": 8,
228228
"metadata": {},
229229
"outputs": [
230230
{
231231
"name": "stdout",
232232
"output_type": "stream",
233233
"text": [
234-
"4083964757.425642\n"
234+
"Channel_000 Channel_001 0\n",
235+
"Channel_000 Channel_001 0\n",
236+
"Channel_001 Channel_001 0\n",
237+
"Channel_001 Channel_001 1\n",
238+
"resolution_function Channel_001 1\n",
239+
"resolution_function Channel_001 1\n",
240+
"plasmon Channel_001 1\n",
241+
"plasmon Channel_001 1\n",
242+
"multiple_scattering Channel_001 1\n",
243+
"multiple_scattering Channel_001 1\n"
235244
]
236245
}
237246
],
238247
"source": [
239-
"infoWidget.low_loss.get_drude()\n"
248+
"infoWidget.tab_buttons.index = 2\n",
249+
"\n",
250+
"infoWidget.low_loss.update_ll_sidebar()\n"
251+
]
252+
},
253+
{
254+
"cell_type": "code",
255+
"execution_count": 121,
256+
"metadata": {},
257+
"outputs": [],
258+
"source": [
259+
"infoWidget.tabval = 2\n",
260+
"infoWidget.tab_activated()"
261+
]
262+
},
263+
{
264+
"cell_type": "code",
265+
"execution_count": 46,
266+
"metadata": {},
267+
"outputs": [],
268+
"source": [
269+
"import ipywidgets\n",
270+
"infoWidget.tab.children = [infoWidget.tab_buttons, infoWidget.children[infoWidget.tab_buttons.index]]"
271+
]
272+
},
273+
{
274+
"cell_type": "code",
275+
"execution_count": 56,
276+
"metadata": {},
277+
"outputs": [],
278+
"source": [
279+
"infoWidget.info.update_dataset()"
240280
]
241281
},
242282
{
@@ -967,7 +1007,9 @@
9671007
},
9681008
{
9691009
"cell_type": "markdown",
970-
"metadata": {},
1010+
"metadata": {
1011+
"jp-MarkdownHeadingCollapsed": true
1012+
},
9711013
"source": [
9721014
"## peakfit"
9731015
]

pyTEMlib/info_widget.py

Lines changed: 47 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import numpy as np
44
import os
5+
import sys
56
import ipywidgets
67
import matplotlib.pylab as plt
78
import matplotlib
@@ -253,6 +254,11 @@ def __init__(self, datasets, sidebar, tab_title=None):
253254
self.key = None
254255
self.new_info = False
255256
self.image = 'Sum'
257+
if 'google.colab' in sys.modules:
258+
self.google = True
259+
else:
260+
self.google = False
261+
self.google = True
256262

257263
self.save_path = True
258264

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

277283
self.file_bar = get_file_widget_ui()
284+
children = [self.file_bar]
285+
titles = ['File']
278286
if isinstance(sidebar, dict):
279-
tab = ipywidgets.Tab()
280-
children = [self.file_bar]
281-
titles = ['File']
287+
282288
for sidebar_key, sidebar_gui in sidebar.items():
283289
children.append(sidebar_gui)
284290
titles.append(sidebar_key)
285-
tab.children = children
286-
tab.titles = titles
287291
elif not isinstance(sidebar, list):
288-
tab = ipywidgets.Tab()
289-
tab.children = [self.file_bar, sidebar]
290-
tab.titles = ['File', 'Info']
292+
children = [self.file_bar, sidebar]
293+
titles = ['File', 'Info']
294+
295+
if self.google:
296+
self.buttons = []
297+
for i in range(len(children)):
298+
self.buttons.append(ipywidgets.Button(description=titles[i],
299+
disabled=False,
300+
button_style='', # 'success', 'info', 'warning', 'danger' or ''
301+
layout=ipywidgets.Layout(width='800px')))
302+
303+
304+
self.tab_buttons = ipywidgets.ToggleButtons(options=titles, description='', disabled=False,
305+
layout=ipywidgets.Layout(width='auto'),
306+
style={"button_width": "auto"})
307+
tab = ipywidgets.VBox([self.tab_buttons, self.file_bar])
308+
self.children = children
309+
291310
else:
292-
tab = sidebar
293-
self.tab = tab
311+
tab = ipywidgets.Tab()
312+
tab.children = children
313+
tab.titles = titles
314+
294315
with plt.ioff():
295316
self.figure = plt.figure()
296-
317+
self.tab =tab
297318
self.figure.canvas.toolbar_position = 'right'
298319
self.figure.canvas.toolbar_visible = True
299320

@@ -739,18 +760,25 @@ def __init__(self, datasets=None):
739760
self.set_action()
740761

741762
def set_action(self):
763+
if self.google:
764+
self.tab_buttons.observe(self.tab_activated)
742765
self.tab.observe(self.tab_activated)
743766

744767
def tab_activated(self, val=0):
745-
if isinstance(val.new, int):
746-
self.tabval = val.new
768+
if self.google:
769+
self.tab.children = [self.tab_buttons, self.children[self.tab_buttons.index]]
770+
self.tabval = self.tab_buttons.index
771+
else:
772+
if isinstance(val.new, int):
773+
self.tabval = val.new
747774
# self.update_sidebars()
748-
if val.new == 1:
749-
self.info.update_dataset()
750-
elif val.new == 2:
751-
self.low_loss.update_ll_sidebar()
752-
elif val.new == 3:
753-
self.core_loss.update_cl_sidebar()
775+
if self.tabval == 1:
776+
self.info.update_dataset()
777+
elif self.tabval == 2:
778+
self.low_loss.update_ll_sidebar()
779+
elif self.tabval == 3:
780+
self.core_loss.update_cl_sidebar()
781+
754782

755783
def update_sidebars(self):
756784
if hasattr(self, 'info'):

pyTEMlib/low_loss_widget.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,9 +132,14 @@ def update_ll_sidebar(self):
132132
if 'SPECTR' in self.parent.datasets[key].data_type.name:
133133
energy_offset = self.parent.datasets[key].get_spectral_dims(return_axis=True)[0][0]
134134
if energy_offset < 0:
135-
spectrum_list.append(f'{key}: {self.parent.datasets[key].title}')
135+
spectrum_list.append(f'{key}: {self.parent.datasets[key].title}')
136+
print(key, self.ll_key, ll_index)
136137
if key == self.ll_key:
137-
ll_index = index+1
138+
ll_index = index-1
139+
print(key, self.ll_key, ll_index)
140+
if ll_index >len(spectrum_list) - 1:
141+
ll_index = len(spectrum_list) - 1
142+
138143
self.low_loss_tab[0, 0].options = spectrum_list
139144
self.low_loss_tab[0, 0].value = spectrum_list[ll_index]
140145

0 commit comments

Comments
 (0)