pandas_GUI.utils
pandas_GUI.utils The Jupyter JS call utils below are being deprecated by utilites in the package JPSLUtils (https://github.com/JupyterPhysSciLab/JPSLUtils).
1""" pandas_GUI.utils 2The Jupyter JS call utils below are being deprecated by utilites in the 3package JPSLUtils (https://github.com/JupyterPhysSciLab/JPSLUtils). 4""" 5import ipywidgets 6 7###### 8# Jupyter JS call utilities 9###### 10def new_cell_immediately_below(): 11 from IPython.display import display, HTML 12 from IPython.display import Javascript as JS 13 display( 14 JS('Jupyter.notebook.focus_cell();' \ 15 'Jupyter.notebook.insert_cell_below();')) 16 pass 17 18 19def select_cell_immediately_below(): 20 from IPython.display import display, HTML 21 from IPython.display import Javascript as JS 22 display(JS('Jupyter.notebook.select_next(true);')) 23 24 25def move_cursor_in_current_cell(delta): 26 from IPython.display import display, HTML 27 from IPython.display import Javascript as JS 28 display( 29 JS('var curPos = Jupyter.notebook.get_selected_cell().code_' \ 30 'mirror.doc.getCursor();' \ 31 'var curline = curPos.line; var curch = curPos.ch +' + str( 32 delta) + ';' \ 33 'Jupyter.notebook.get_selected_cell().code_mirror.' \ 34 'doc.setCursor({line:curline,ch:curch});')) 35 pass 36 37 38def insert_text_into_next_cell(text): 39 from IPython.display import display, HTML 40 from IPython.display import Javascript as JS 41 display(JS('Jupyter.notebook.select_next(true);' \ 42 'Jupyter.notebook.get_selected_cell().code_mirror.doc.' \ 43 'replaceSelection("' + text + '");')) 44 pass 45 46def replace_text_of_current_cell(text): 47 from IPython.display import display, HTML 48 from IPython.display import Javascript as JS 49 display(JS('Jupyter.notebook.get_selected_cell().set_text("' + text +'");')) 50 51 52def insert_text_at_beginning_of_current_cell(text): 53 # append \n to line insert as a separate line. 54 from IPython.display import display, HTML 55 from IPython.display import Javascript as JS 56 display( 57 JS('Jupyter.notebook.get_selected_cell().code_mirror.doc.' \ 58 'setCursor({line:0,ch:0});' \ 59 'Jupyter.notebook.get_selected_cell().code_mirror.doc.' \ 60 'replaceSelection("' + text + '");')) 61 pass 62 63 64def insert_newline_at_end_of_current_cell(text): 65 from IPython.display import display, HTML 66 from IPython.display import Javascript as JS 67 display( 68 JS('var lastline = Jupyter.notebook.get_selected_cell().' \ 69 'code_mirror.doc.lineCount();' \ 70 'Jupyter.notebook.get_selected_cell().code_mirror.doc.' \ 71 'setCursor(lastline,0);' \ 72 'Jupyter.notebook.get_selected_cell().code_mirror.doc.' \ 73 'replaceSelection("\\n' + text + '");')) 74 pass 75 76def select_containing_cell(elemID): 77 from IPython.display import display, HTML 78 from IPython.display import Javascript as JS 79 # Create a synthetic click in the cell to force selection of the cell 80 # containing the table 81 display(JS( 82 'var event = new MouseEvent("click", {' \ 83 'view: window,' \ 84 'bubbles: true,' \ 85 'cancelable: true' \ 86 '});' \ 87 'var start = new Date().getTime();' \ 88 'var elem = document.getElementById("'+elemID+'");' \ 89 'do {' \ 90 'elem = document.getElementById("'+elemID+'");' \ 91 '} while ((elem == null) && (new Date().getTime() < start+5000));' \ 92 'if (elem == null){' \ 93 'alert("It took more than 5 seconds to build element.");}' \ 94 'var cancelled = !elem.dispatchEvent(event);' \ 95 'if (cancelled) {' \ 96 # A handler called preventDefault. 97 'alert("Something is wrong. Try running the cell that creates this GUI' \ 98 '.");' \ 99 '}')) 100 pass 101 102def delete_selected_cell(): 103 from IPython.display import display, HTML 104 from IPython.display import Javascript as JS 105 display(JS('Jupyter.notebook.delete_cell(' \ 106 'Jupyter.notebook.get_selected_index());')) 107 pass 108 109###### 110# Pandas and Figures routines 111###### 112 113def find_pandas_dataframe_names(): 114 """ 115 This operation will search the interactive name space for pandas 116 DataFrame objects. It will not find DataFrames that are children 117 of objects in the interactive namespace. You will need to provide 118 your own operation for finding those. 119 120 :return list: string names for objects in the global interactive 121 namespace that are pandas DataFrames. 122 """ 123 from pandas import DataFrame as df 124 from IPython import get_ipython 125 126 dataframenames = [] 127 global_dict = get_ipython().user_ns 128 for k in global_dict: 129 if not (str.startswith(k, '_')) and isinstance(global_dict[k], df): 130 dataframenames.append(k) 131 return dataframenames 132 133def find_figure_names(): 134 """ 135 This operation will search the interactive namespace for objects that are 136 plotly Figures (plotly.graph_objects.Figure) or plotly FigureWidgets 137 (plotly.graph_objects.FigureWidget). It will not find Figures or 138 FigureWidgets that are children of other objects. You will need to 139 provide your own operation for finding those. 140 141 :return list: of string names for the objects in the global 142 interactive namespace that are plotly Figures or FigureWidgets. 143 """ 144 from plotly.graph_objects import Figure, FigureWidget 145 from IPython import get_ipython 146 147 fignames = [] 148 global_dict = get_ipython().user_ns 149 for k in global_dict: 150 if not (str.startswith(k, '_')) and isinstance(global_dict[k], 151 (Figure,FigureWidget)): 152 fignames.append(k) 153 return fignames 154 155def find_fit_names(): 156 """ 157 This operation will search the interactive namespace for objects that are 158 lmfit results (lmfit.model.ModelResults). It will not find fit results 159 that are children of other objects. You will need to 160 provide your own operation for finding those. 161 162 :return list: of string names for the objects in the global 163 interactive namespace that are lmfit fit results. 164 """ 165 from lmfit.model import ModelResult 166 from IPython import get_ipython 167 168 fitnames = [] 169 global_dict = get_ipython().user_ns 170 for k in global_dict: 171 if not (str.startswith(k, '_')) and isinstance(global_dict[k], 172 ModelResult): 173 fitnames.append(k) 174 return fitnames 175 176class iconselector(): 177 """ 178 This class provides a self updating set of small buttons showing the 179 font-awesome icons passed to it. The user selected icon is highlighted 180 in darkgray. The `selected` attribute (value is a synonym) is set to the 181 name of the current selection. The `box` attribute is an ipywidget HBox 182 that can be displayed or incorporated into more complex ipywidget 183 constructs to interact with the user. 184 """ 185 ##### 186 # TODO: add .observe option to icon selector...change object to extend 187 # the appropriate widget type? 188 ##### 189 def __init__(self,iconlist, selected = None): 190 """ 191 192 :param list iconlist: list of string names for the font awsome icons to 193 display. The names should not be prefixed with 'fa-'. 194 195 :param string selected: name of selected icon (default = None). 196 """ 197 from ipywidgets import HBox, Button, Layout 198 self.buttons = [] 199 self.selected = selected # This will be the selected icon name 200 201 def iconbutclk(but): 202 self.selected = but.icon 203 for k in self.buttons: 204 if k.icon != self.selected: 205 k.style.button_color = 'white' 206 else: 207 k.style.button_color = 'darkgray' 208 pass 209 210 smallbut = Layout(width='30px') 211 for k in iconlist: 212 tempbut = Button(icon=k,layout=smallbut) 213 tempbut.style.button_color = 'white' 214 tempbut.style.boarder = 'none' 215 tempbut.on_click(iconbutclk) 216 self.buttons.append(tempbut) 217 if self.selected != None: 218 for k in self.buttons: 219 if k.icon == self.selected: 220 iconbutclk(k) 221 self.box = HBox(self.buttons) # This can be passed as a widget. 222 223 @property 224 def value(self): 225 return self.selected 226 227class notice_group(): 228 """ 229 A notice group contains a list of strings that are referred to by their 230 index. The group keeps track of which notices are 'active'. A call to the 231 `.notice_html()` method returns an unordered html formatted list of the 232 notice texts. This can be used to display or update notice text 233 for the user. 234 235 Optional notice group color, header and footers can be provided. 236 """ 237 def __init__(self, noticelist, header='', footer = '', color = ''): 238 """ 239 240 :param list noticelist: list of strings of the text for each notice 241 242 :param string header: string providing a header for this notice group 243 244 :param string footer: string providing a footer for this notice group 245 246 :param string color: string compatible with css color attribute, 247 used to color the displayed notices. The color not impact headers 248 and footers. 249 """ 250 self.header = header 251 self.noticelist = noticelist 252 self.footer = footer 253 self.color = color 254 self.active = [] 255 256 def get_active(self): 257 """Returns a list of indexes of active notices""" 258 return self.active 259 260 def set_active(self,whichnotices): 261 """ 262 Used to set a specific list of notices to active. This will remove 263 active notices that are not in the provided list. 264 265 :param list whichnotices: 266 """ 267 self.active = whichnotices 268 pass 269 270 def activate_notice(self, notice_id): 271 """ 272 adds one of the notices to the active list 273 274 :param int notice_id: 275 """ 276 if notice_id not in self.active: 277 self.active.append(notice_id) 278 pass 279 280 def deactivate_notice(self, notice_id): 281 """ 282 removes a notice from the active list 283 284 :param int notice_id: 285 """ 286 if notice_id in self.active: 287 self.active.remove(notice_id) 288 pass 289 290 def notice_html(self): 291 """ 292 Provides an html formatted string displaying the active notices. 293 294 :return string: string of html. 295 """ 296 notice_header = '' 297 if self.header !='': 298 notice_header = '<h4 style="text-align:center;">'+self.header+\ 299 ' </h4><ul>' 300 notice_footer = self.footer+'</ul>' 301 notice_txt = notice_header 302 itemstart = '<li style="color:'+self.color+';">' 303 for j in self.active: 304 notice_txt += itemstart + self.noticelist[j]+'</li>' 305 notice_txt += notice_footer 306 return notice_txt 307 308class build_run_snip_widget(ipywidgets.GridBox): 309 310 def __init__(self, defaulttxt, output_elem): 311 """ 312 Defines a widget that runs code built in it and replaces itself with 313 the results. 314 :param defaulttxt: Initial text in the codebox 315 :param output_elem: Where this element will be located (an ipywidget 316 `Output()` element). Must be created before creating this object. 317 """ 318 from ipywidgets import Textarea, Layout, Button, VBox, GridBox 319 from ipywidgets import HTML as richLabel 320 from IPython.display import display, HTML, clear_output 321 from IPython import get_ipython 322 self.run_env = (get_ipython().user_ns["JPSLUtils"]).notebookenv 323 self.sniptext = Textarea( 324 layout=Layout(width='98%', height='200px'), 325 value=defaulttxt 326 ) 327 self.value = self.sniptext.value 328 self.dobutton = Button(description='Run Code') 329 instr_str = '<div style="line-height:1;">' 330 if self.run_env == 'colab': 331 instr_str += \ 332 '<p>You appear to be running in Google Colabratory. When done ' \ 333 'working through all the steps, copy the code at left into a ' \ 334 'new code cell to run it. In Jupyter lab and classic Jupyter ' \ 335 'notebooks this can be done automatically for you.</p>' 336 else: 337 instr_str += \ 338 '<p>You appear to be running in Jupyter lab. When done ' \ 339 'working through all the steps, clicking on the "Run Code" ' \ 340 'button will replace the GUI with the results of running the ' \ 341 'code. Copying the code in the collapsed "Code that was run" ' \ 342 'summary into a code cell will prevent the code from being ' \ 343 'lost if outputs are cleared.</p>' 344 instr_str += '<br/><p>POWER USER HINT: You can repeatedly try ' \ 345 'different settings by copying the completed code from ' \ 346 'the text box at left into a code cell. Then run it. ' \ 347 'Then use the GUI tools to update the code at left and ' \ 348 'try again.</p></div>' 349 self.instructions = richLabel(value = instr_str) 350 if self.run_env == 'colab': 351 self.dobox = VBox([self.instructions]) 352 else: 353 self.dobox = VBox([self.dobutton,self.instructions]) 354 def onRunCode(change): 355 from IPython import get_ipython 356 shell = get_ipython() 357 output_elem.clear_output() 358 with output_elem: 359 display(HTML( 360 '<details><summary style="cursor:pointer;"><span style="font-weight:bold;"><a>' \ 361 'Code that was run</a></span>(click to toggle visibility)</summary>' \ 362 '<div style="background:#eff0f1;white-space:pre-line;white-space:pre-wrap;">' \ 363 '<code><xmp>' + self.sniptext.value 364 +'</xmp></code></div></details>')) 365 display(HTML('<h3>Result<h3>')) 366 exec(str(self.sniptext.value), shell.user_ns) 367 pass 368 369 self.dobutton.on_click(onRunCode) 370 371 super(build_run_snip_widget, self).__init__([self.sniptext, 372 self.dobox], 373 layout=Layout( 374 grid_template_rows='auto', 375 grid_template_columns='75% 25%', 376 grid_template_areas="self.sniptext self.dobox"))
26def move_cursor_in_current_cell(delta): 27 from IPython.display import display, HTML 28 from IPython.display import Javascript as JS 29 display( 30 JS('var curPos = Jupyter.notebook.get_selected_cell().code_' \ 31 'mirror.doc.getCursor();' \ 32 'var curline = curPos.line; var curch = curPos.ch +' + str( 33 delta) + ';' \ 34 'Jupyter.notebook.get_selected_cell().code_mirror.' \ 35 'doc.setCursor({line:curline,ch:curch});')) 36 pass
53def insert_text_at_beginning_of_current_cell(text): 54 # append \n to line insert as a separate line. 55 from IPython.display import display, HTML 56 from IPython.display import Javascript as JS 57 display( 58 JS('Jupyter.notebook.get_selected_cell().code_mirror.doc.' \ 59 'setCursor({line:0,ch:0});' \ 60 'Jupyter.notebook.get_selected_cell().code_mirror.doc.' \ 61 'replaceSelection("' + text + '");')) 62 pass
65def insert_newline_at_end_of_current_cell(text): 66 from IPython.display import display, HTML 67 from IPython.display import Javascript as JS 68 display( 69 JS('var lastline = Jupyter.notebook.get_selected_cell().' \ 70 'code_mirror.doc.lineCount();' \ 71 'Jupyter.notebook.get_selected_cell().code_mirror.doc.' \ 72 'setCursor(lastline,0);' \ 73 'Jupyter.notebook.get_selected_cell().code_mirror.doc.' \ 74 'replaceSelection("\\n' + text + '");')) 75 pass
77def select_containing_cell(elemID): 78 from IPython.display import display, HTML 79 from IPython.display import Javascript as JS 80 # Create a synthetic click in the cell to force selection of the cell 81 # containing the table 82 display(JS( 83 'var event = new MouseEvent("click", {' \ 84 'view: window,' \ 85 'bubbles: true,' \ 86 'cancelable: true' \ 87 '});' \ 88 'var start = new Date().getTime();' \ 89 'var elem = document.getElementById("'+elemID+'");' \ 90 'do {' \ 91 'elem = document.getElementById("'+elemID+'");' \ 92 '} while ((elem == null) && (new Date().getTime() < start+5000));' \ 93 'if (elem == null){' \ 94 'alert("It took more than 5 seconds to build element.");}' \ 95 'var cancelled = !elem.dispatchEvent(event);' \ 96 'if (cancelled) {' \ 97 # A handler called preventDefault. 98 'alert("Something is wrong. Try running the cell that creates this GUI' \ 99 '.");' \ 100 '}')) 101 pass
114def find_pandas_dataframe_names(): 115 """ 116 This operation will search the interactive name space for pandas 117 DataFrame objects. It will not find DataFrames that are children 118 of objects in the interactive namespace. You will need to provide 119 your own operation for finding those. 120 121 :return list: string names for objects in the global interactive 122 namespace that are pandas DataFrames. 123 """ 124 from pandas import DataFrame as df 125 from IPython import get_ipython 126 127 dataframenames = [] 128 global_dict = get_ipython().user_ns 129 for k in global_dict: 130 if not (str.startswith(k, '_')) and isinstance(global_dict[k], df): 131 dataframenames.append(k) 132 return dataframenames
This operation will search the interactive name space for pandas DataFrame objects. It will not find DataFrames that are children of objects in the interactive namespace. You will need to provide your own operation for finding those.
Returns
string names for objects in the global interactive namespace that are pandas DataFrames.
134def find_figure_names(): 135 """ 136 This operation will search the interactive namespace for objects that are 137 plotly Figures (plotly.graph_objects.Figure) or plotly FigureWidgets 138 (plotly.graph_objects.FigureWidget). It will not find Figures or 139 FigureWidgets that are children of other objects. You will need to 140 provide your own operation for finding those. 141 142 :return list: of string names for the objects in the global 143 interactive namespace that are plotly Figures or FigureWidgets. 144 """ 145 from plotly.graph_objects import Figure, FigureWidget 146 from IPython import get_ipython 147 148 fignames = [] 149 global_dict = get_ipython().user_ns 150 for k in global_dict: 151 if not (str.startswith(k, '_')) and isinstance(global_dict[k], 152 (Figure,FigureWidget)): 153 fignames.append(k) 154 return fignames
This operation will search the interactive namespace for objects that are plotly Figures (plotly.graph_objects.Figure) or plotly FigureWidgets (plotly.graph_objects.FigureWidget). It will not find Figures or FigureWidgets that are children of other objects. You will need to provide your own operation for finding those.
Returns
of string names for the objects in the global interactive namespace that are plotly Figures or FigureWidgets.
156def find_fit_names(): 157 """ 158 This operation will search the interactive namespace for objects that are 159 lmfit results (lmfit.model.ModelResults). It will not find fit results 160 that are children of other objects. You will need to 161 provide your own operation for finding those. 162 163 :return list: of string names for the objects in the global 164 interactive namespace that are lmfit fit results. 165 """ 166 from lmfit.model import ModelResult 167 from IPython import get_ipython 168 169 fitnames = [] 170 global_dict = get_ipython().user_ns 171 for k in global_dict: 172 if not (str.startswith(k, '_')) and isinstance(global_dict[k], 173 ModelResult): 174 fitnames.append(k) 175 return fitnames
This operation will search the interactive namespace for objects that are lmfit results (lmfit.model.ModelResults). It will not find fit results that are children of other objects. You will need to provide your own operation for finding those.
Returns
of string names for the objects in the global interactive namespace that are lmfit fit results.
177class iconselector(): 178 """ 179 This class provides a self updating set of small buttons showing the 180 font-awesome icons passed to it. The user selected icon is highlighted 181 in darkgray. The `selected` attribute (value is a synonym) is set to the 182 name of the current selection. The `box` attribute is an ipywidget HBox 183 that can be displayed or incorporated into more complex ipywidget 184 constructs to interact with the user. 185 """ 186 ##### 187 # TODO: add .observe option to icon selector...change object to extend 188 # the appropriate widget type? 189 ##### 190 def __init__(self,iconlist, selected = None): 191 """ 192 193 :param list iconlist: list of string names for the font awsome icons to 194 display. The names should not be prefixed with 'fa-'. 195 196 :param string selected: name of selected icon (default = None). 197 """ 198 from ipywidgets import HBox, Button, Layout 199 self.buttons = [] 200 self.selected = selected # This will be the selected icon name 201 202 def iconbutclk(but): 203 self.selected = but.icon 204 for k in self.buttons: 205 if k.icon != self.selected: 206 k.style.button_color = 'white' 207 else: 208 k.style.button_color = 'darkgray' 209 pass 210 211 smallbut = Layout(width='30px') 212 for k in iconlist: 213 tempbut = Button(icon=k,layout=smallbut) 214 tempbut.style.button_color = 'white' 215 tempbut.style.boarder = 'none' 216 tempbut.on_click(iconbutclk) 217 self.buttons.append(tempbut) 218 if self.selected != None: 219 for k in self.buttons: 220 if k.icon == self.selected: 221 iconbutclk(k) 222 self.box = HBox(self.buttons) # This can be passed as a widget. 223 224 @property 225 def value(self): 226 return self.selected
This class provides a self updating set of small buttons showing the
font-awesome icons passed to it. The user selected icon is highlighted
in darkgray. The selected
attribute (value is a synonym) is set to the
name of the current selection. The box
attribute is an ipywidget HBox
that can be displayed or incorporated into more complex ipywidget
constructs to interact with the user.
190 def __init__(self,iconlist, selected = None): 191 """ 192 193 :param list iconlist: list of string names for the font awsome icons to 194 display. The names should not be prefixed with 'fa-'. 195 196 :param string selected: name of selected icon (default = None). 197 """ 198 from ipywidgets import HBox, Button, Layout 199 self.buttons = [] 200 self.selected = selected # This will be the selected icon name 201 202 def iconbutclk(but): 203 self.selected = but.icon 204 for k in self.buttons: 205 if k.icon != self.selected: 206 k.style.button_color = 'white' 207 else: 208 k.style.button_color = 'darkgray' 209 pass 210 211 smallbut = Layout(width='30px') 212 for k in iconlist: 213 tempbut = Button(icon=k,layout=smallbut) 214 tempbut.style.button_color = 'white' 215 tempbut.style.boarder = 'none' 216 tempbut.on_click(iconbutclk) 217 self.buttons.append(tempbut) 218 if self.selected != None: 219 for k in self.buttons: 220 if k.icon == self.selected: 221 iconbutclk(k) 222 self.box = HBox(self.buttons) # This can be passed as a widget.
Parameters
list iconlist: list of string names for the font awsome icons to display. The names should not be prefixed with 'fa-'.
string selected: name of selected icon (default = None).
228class notice_group(): 229 """ 230 A notice group contains a list of strings that are referred to by their 231 index. The group keeps track of which notices are 'active'. A call to the 232 `.notice_html()` method returns an unordered html formatted list of the 233 notice texts. This can be used to display or update notice text 234 for the user. 235 236 Optional notice group color, header and footers can be provided. 237 """ 238 def __init__(self, noticelist, header='', footer = '', color = ''): 239 """ 240 241 :param list noticelist: list of strings of the text for each notice 242 243 :param string header: string providing a header for this notice group 244 245 :param string footer: string providing a footer for this notice group 246 247 :param string color: string compatible with css color attribute, 248 used to color the displayed notices. The color not impact headers 249 and footers. 250 """ 251 self.header = header 252 self.noticelist = noticelist 253 self.footer = footer 254 self.color = color 255 self.active = [] 256 257 def get_active(self): 258 """Returns a list of indexes of active notices""" 259 return self.active 260 261 def set_active(self,whichnotices): 262 """ 263 Used to set a specific list of notices to active. This will remove 264 active notices that are not in the provided list. 265 266 :param list whichnotices: 267 """ 268 self.active = whichnotices 269 pass 270 271 def activate_notice(self, notice_id): 272 """ 273 adds one of the notices to the active list 274 275 :param int notice_id: 276 """ 277 if notice_id not in self.active: 278 self.active.append(notice_id) 279 pass 280 281 def deactivate_notice(self, notice_id): 282 """ 283 removes a notice from the active list 284 285 :param int notice_id: 286 """ 287 if notice_id in self.active: 288 self.active.remove(notice_id) 289 pass 290 291 def notice_html(self): 292 """ 293 Provides an html formatted string displaying the active notices. 294 295 :return string: string of html. 296 """ 297 notice_header = '' 298 if self.header !='': 299 notice_header = '<h4 style="text-align:center;">'+self.header+\ 300 ' </h4><ul>' 301 notice_footer = self.footer+'</ul>' 302 notice_txt = notice_header 303 itemstart = '<li style="color:'+self.color+';">' 304 for j in self.active: 305 notice_txt += itemstart + self.noticelist[j]+'</li>' 306 notice_txt += notice_footer 307 return notice_txt
A notice group contains a list of strings that are referred to by their
index. The group keeps track of which notices are 'active'. A call to the
.notice_html()
method returns an unordered html formatted list of the
notice texts. This can be used to display or update notice text
for the user.
Optional notice group color, header and footers can be provided.
238 def __init__(self, noticelist, header='', footer = '', color = ''): 239 """ 240 241 :param list noticelist: list of strings of the text for each notice 242 243 :param string header: string providing a header for this notice group 244 245 :param string footer: string providing a footer for this notice group 246 247 :param string color: string compatible with css color attribute, 248 used to color the displayed notices. The color not impact headers 249 and footers. 250 """ 251 self.header = header 252 self.noticelist = noticelist 253 self.footer = footer 254 self.color = color 255 self.active = []
Parameters
list noticelist: list of strings of the text for each notice
string header: string providing a header for this notice group
string footer: string providing a footer for this notice group
string color: string compatible with css color attribute, used to color the displayed notices. The color not impact headers and footers.
257 def get_active(self): 258 """Returns a list of indexes of active notices""" 259 return self.active
Returns a list of indexes of active notices
261 def set_active(self,whichnotices): 262 """ 263 Used to set a specific list of notices to active. This will remove 264 active notices that are not in the provided list. 265 266 :param list whichnotices: 267 """ 268 self.active = whichnotices 269 pass
Used to set a specific list of notices to active. This will remove active notices that are not in the provided list.
Parameters
- list whichnotices:
271 def activate_notice(self, notice_id): 272 """ 273 adds one of the notices to the active list 274 275 :param int notice_id: 276 """ 277 if notice_id not in self.active: 278 self.active.append(notice_id) 279 pass
adds one of the notices to the active list
Parameters
- int notice_id:
281 def deactivate_notice(self, notice_id): 282 """ 283 removes a notice from the active list 284 285 :param int notice_id: 286 """ 287 if notice_id in self.active: 288 self.active.remove(notice_id) 289 pass
removes a notice from the active list
Parameters
- int notice_id:
291 def notice_html(self): 292 """ 293 Provides an html formatted string displaying the active notices. 294 295 :return string: string of html. 296 """ 297 notice_header = '' 298 if self.header !='': 299 notice_header = '<h4 style="text-align:center;">'+self.header+\ 300 ' </h4><ul>' 301 notice_footer = self.footer+'</ul>' 302 notice_txt = notice_header 303 itemstart = '<li style="color:'+self.color+';">' 304 for j in self.active: 305 notice_txt += itemstart + self.noticelist[j]+'</li>' 306 notice_txt += notice_footer 307 return notice_txt
Provides an html formatted string displaying the active notices.
Returns
string of html.
309class build_run_snip_widget(ipywidgets.GridBox): 310 311 def __init__(self, defaulttxt, output_elem): 312 """ 313 Defines a widget that runs code built in it and replaces itself with 314 the results. 315 :param defaulttxt: Initial text in the codebox 316 :param output_elem: Where this element will be located (an ipywidget 317 `Output()` element). Must be created before creating this object. 318 """ 319 from ipywidgets import Textarea, Layout, Button, VBox, GridBox 320 from ipywidgets import HTML as richLabel 321 from IPython.display import display, HTML, clear_output 322 from IPython import get_ipython 323 self.run_env = (get_ipython().user_ns["JPSLUtils"]).notebookenv 324 self.sniptext = Textarea( 325 layout=Layout(width='98%', height='200px'), 326 value=defaulttxt 327 ) 328 self.value = self.sniptext.value 329 self.dobutton = Button(description='Run Code') 330 instr_str = '<div style="line-height:1;">' 331 if self.run_env == 'colab': 332 instr_str += \ 333 '<p>You appear to be running in Google Colabratory. When done ' \ 334 'working through all the steps, copy the code at left into a ' \ 335 'new code cell to run it. In Jupyter lab and classic Jupyter ' \ 336 'notebooks this can be done automatically for you.</p>' 337 else: 338 instr_str += \ 339 '<p>You appear to be running in Jupyter lab. When done ' \ 340 'working through all the steps, clicking on the "Run Code" ' \ 341 'button will replace the GUI with the results of running the ' \ 342 'code. Copying the code in the collapsed "Code that was run" ' \ 343 'summary into a code cell will prevent the code from being ' \ 344 'lost if outputs are cleared.</p>' 345 instr_str += '<br/><p>POWER USER HINT: You can repeatedly try ' \ 346 'different settings by copying the completed code from ' \ 347 'the text box at left into a code cell. Then run it. ' \ 348 'Then use the GUI tools to update the code at left and ' \ 349 'try again.</p></div>' 350 self.instructions = richLabel(value = instr_str) 351 if self.run_env == 'colab': 352 self.dobox = VBox([self.instructions]) 353 else: 354 self.dobox = VBox([self.dobutton,self.instructions]) 355 def onRunCode(change): 356 from IPython import get_ipython 357 shell = get_ipython() 358 output_elem.clear_output() 359 with output_elem: 360 display(HTML( 361 '<details><summary style="cursor:pointer;"><span style="font-weight:bold;"><a>' \ 362 'Code that was run</a></span>(click to toggle visibility)</summary>' \ 363 '<div style="background:#eff0f1;white-space:pre-line;white-space:pre-wrap;">' \ 364 '<code><xmp>' + self.sniptext.value 365 +'</xmp></code></div></details>')) 366 display(HTML('<h3>Result<h3>')) 367 exec(str(self.sniptext.value), shell.user_ns) 368 pass 369 370 self.dobutton.on_click(onRunCode) 371 372 super(build_run_snip_widget, self).__init__([self.sniptext, 373 self.dobox], 374 layout=Layout( 375 grid_template_rows='auto', 376 grid_template_columns='75% 25%', 377 grid_template_areas="self.sniptext self.dobox"))
Displays multiple widgets in rows and columns using the grid box model.
Parameters
{box_params}
Examples
>>> import ipywidgets as widgets
>>> title_widget = widgets.HTML('<em>Grid Box Example</em>')
>>> slider = widgets.IntSlider()
>>> button1 = widgets.Button(description='1')
>>> button2 = widgets.Button(description='2')
>>> # Create a grid with two columns, splitting space equally
>>> layout = widgets.Layout(grid_template_columns='1fr 1fr')
>>> widgets.GridBox([title_widget, slider, button1, button2], layout=layout)
311 def __init__(self, defaulttxt, output_elem): 312 """ 313 Defines a widget that runs code built in it and replaces itself with 314 the results. 315 :param defaulttxt: Initial text in the codebox 316 :param output_elem: Where this element will be located (an ipywidget 317 `Output()` element). Must be created before creating this object. 318 """ 319 from ipywidgets import Textarea, Layout, Button, VBox, GridBox 320 from ipywidgets import HTML as richLabel 321 from IPython.display import display, HTML, clear_output 322 from IPython import get_ipython 323 self.run_env = (get_ipython().user_ns["JPSLUtils"]).notebookenv 324 self.sniptext = Textarea( 325 layout=Layout(width='98%', height='200px'), 326 value=defaulttxt 327 ) 328 self.value = self.sniptext.value 329 self.dobutton = Button(description='Run Code') 330 instr_str = '<div style="line-height:1;">' 331 if self.run_env == 'colab': 332 instr_str += \ 333 '<p>You appear to be running in Google Colabratory. When done ' \ 334 'working through all the steps, copy the code at left into a ' \ 335 'new code cell to run it. In Jupyter lab and classic Jupyter ' \ 336 'notebooks this can be done automatically for you.</p>' 337 else: 338 instr_str += \ 339 '<p>You appear to be running in Jupyter lab. When done ' \ 340 'working through all the steps, clicking on the "Run Code" ' \ 341 'button will replace the GUI with the results of running the ' \ 342 'code. Copying the code in the collapsed "Code that was run" ' \ 343 'summary into a code cell will prevent the code from being ' \ 344 'lost if outputs are cleared.</p>' 345 instr_str += '<br/><p>POWER USER HINT: You can repeatedly try ' \ 346 'different settings by copying the completed code from ' \ 347 'the text box at left into a code cell. Then run it. ' \ 348 'Then use the GUI tools to update the code at left and ' \ 349 'try again.</p></div>' 350 self.instructions = richLabel(value = instr_str) 351 if self.run_env == 'colab': 352 self.dobox = VBox([self.instructions]) 353 else: 354 self.dobox = VBox([self.dobutton,self.instructions]) 355 def onRunCode(change): 356 from IPython import get_ipython 357 shell = get_ipython() 358 output_elem.clear_output() 359 with output_elem: 360 display(HTML( 361 '<details><summary style="cursor:pointer;"><span style="font-weight:bold;"><a>' \ 362 'Code that was run</a></span>(click to toggle visibility)</summary>' \ 363 '<div style="background:#eff0f1;white-space:pre-line;white-space:pre-wrap;">' \ 364 '<code><xmp>' + self.sniptext.value 365 +'</xmp></code></div></details>')) 366 display(HTML('<h3>Result<h3>')) 367 exec(str(self.sniptext.value), shell.user_ns) 368 pass 369 370 self.dobutton.on_click(onRunCode) 371 372 super(build_run_snip_widget, self).__init__([self.sniptext, 373 self.dobox], 374 layout=Layout( 375 grid_template_rows='auto', 376 grid_template_columns='75% 25%', 377 grid_template_areas="self.sniptext self.dobox"))
Defines a widget that runs code built in it and replaces itself with the results.
Parameters
- defaulttxt: Initial text in the codebox
- output_elem: Where this element will be located (an ipywidget
Output()
element). Must be created before creating this object.
Inherited Members
- ipywidgets.widgets.widget_box.Box
- children
- box_style
- ipywidgets.widgets.domwidget.DOMWidget
- tabbable
- tooltip
- layout
- add_class
- remove_class
- focus
- blur
- ipywidgets.widgets.widget.Widget
- widgets
- widget_types
- close_all
- on_widget_constructed
- handle_control_comm_opened
- handle_comm_opened
- get_manager_state
- get_view_spec
- comm
- keys
- open
- model_id
- close
- send_state
- get_state
- set_state
- send
- on_msg
- add_traits
- notify_change
- hold_sync
- ipywidgets.widgets.widget.LoggingHasTraits
- log
- traitlets.traitlets.HasTraits
- setup_instance
- cross_validation_lock
- hold_trait_notifications
- on_trait_change
- observe
- unobserve
- unobserve_all
- set_trait
- class_trait_names
- class_traits
- class_own_traits
- has_trait
- trait_has_value
- trait_values
- trait_defaults
- trait_names
- traits
- trait_metadata
- class_own_trait_events
- trait_events