Jupyter interactive widgets
Created on Mon Aug 9 14:46:11 2021
To define Jupyter widgets which accept user input and can update dataframes and show variables.
A user can define new widgets to suit the need for her use case. This requires some familliarity with the ipywidget library https://ipywidgets.readthedocs.io/en/stable/
This module is a revision of the modelwidget module. Modelwidget is still around and is used for legacy and output widgets.
Ipywidgets are used to define widgets which can be used in jupyter notebooks and in shiny.
- Tabwidget and basewidget:
defines a container of widgets.
- updatawidget:
defines a widget to update variables using the widgets defined in the classes mentioned abowe
- keep_plot_widget:
Defines a widget to display the results from the model run made in the updatewidget
- sheetwidget slidewidget ect:
defines a basic widget which can be dispayed and which might have the ability to update a dataframe
- Some methods and properties of widgets.
- datawidget:
The ipywidget which can be dispayed and might accept input used for updating a dataframe
- datachildren:
A list of widgets in a container widget
- update_df:
a function which updates a dataframe
- reset:
A function which resets the input values to the initial values.
@author: Ib
modelwidget_input
- class modelwidget_input.singelwidget(widgetdef)[source]
Bases:
objectParent class for widgets
Will do some common stuff
Some common properties and methods are defines.
Sets these properties:
self.widgetdef = widgetdef
self.content = self.widgetdef[‘content’] the content of this widget
self heading = self.widgetdef[‘heading’] the heading for this widget.
- Parameters:
widgetdef (dict) – A dict with the descrition of a widget
- Return type:
None.
- class modelwidget_input.basewidget(widgetdef)[source]
Bases:
singelwidgetbasis for widget updating in jupyter
Some common properties and methods are defines.
Sets these properties:
self.widgetdef = widgetdef
self.content = self.widgetdef[‘content’] the content of this widget
self heading = self.widgetdef[‘heading’] the heading for this widget.
- Parameters:
widgetdef (dict) – A dict with the descrition of a widget
- Return type:
None.
- class modelwidget_input.tabwidget(widgetdef)[source]
Bases:
singelwidgetA widget to create tab or acordium contaners
Some common properties and methods are defines.
Sets these properties:
self.widgetdef = widgetdef
self.content = self.widgetdef[‘content’] the content of this widget
self heading = self.widgetdef[‘heading’] the heading for this widget.
- Parameters:
widgetdef (dict) – A dict with the descrition of a widget
- Return type:
None.
- class modelwidget_input.sheetwidget(widgetdef)[source]
Bases:
singelwidgetclass defefining a widget with lines of slides
Some common properties and methods are defines.
Sets these properties:
self.widgetdef = widgetdef
self.content = self.widgetdef[‘content’] the content of this widget
self heading = self.widgetdef[‘heading’] the heading for this widget.
- Parameters:
widgetdef (dict) – A dict with the descrition of a widget
- Return type:
None.
- class modelwidget_input.slidewidget(widgetdef)[source]
Bases:
singelwidgetclass defefining a widget with lines of slides
Some common properties and methods are defines.
Sets these properties:
self.widgetdef = widgetdef
self.content = self.widgetdef[‘content’] the content of this widget
self heading = self.widgetdef[‘heading’] the heading for this widget.
- Parameters:
widgetdef (dict) – A dict with the descrition of a widget
- Return type:
None.
- class modelwidget_input.sumslidewidget(widgetdef)[source]
Bases:
singelwidgetclass defefining a widget with lines of slides the sum of the input variables can max be the parameter maxsum (default=1.0)
Some common properties and methods are defines.
Sets these properties:
self.widgetdef = widgetdef
self.content = self.widgetdef[‘content’] the content of this widget
self heading = self.widgetdef[‘heading’] the heading for this widget.
- Parameters:
widgetdef (dict) – A dict with the descrition of a widget
- Return type:
None.
- class modelwidget_input.radiowidget(widgetdef)[source]
Bases:
singelwidgetclass defining a widget with a number of radiobutton groups
When df_update is executed the cheked variable will be set to 1 the other to 0
- {‘<heading 1> [[‘text1’,’variable1’],
- [‘text2’,’variable2’]
…..]
- ‘heading 2’ [[‘text1’,’variable1’],
- [‘text2’,’variable2’]
…..]
}
Some common properties and methods are defines.
Sets these properties:
self.widgetdef = widgetdef
self.content = self.widgetdef[‘content’] the content of this widget
self heading = self.widgetdef[‘heading’] the heading for this widget.
- Parameters:
widgetdef (dict) – A dict with the descrition of a widget
- Return type:
None.
- class modelwidget_input.checkwidget(widgetdef)[source]
Bases:
singelwidgetclass defefining a widget with a number of radiobutton groups
When df_update is executed the cheked variable will be set to 1 the other to 0
- {
‘<heading 1>:[‘variable1 variable2 —‘,value ], ‘<heading 2>:[‘variable10 variable20 —‘,value ] }
Some common properties and methods are defines.
Sets these properties:
self.widgetdef = widgetdef
self.content = self.widgetdef[‘content’] the content of this widget
self heading = self.widgetdef[‘heading’] the heading for this widget.
- Parameters:
widgetdef (dict) – A dict with the descrition of a widget
- Return type:
None.
- class modelwidget_input.updatewidget(mmodel: any, datawidget: any, basename: str = 'Business as usual', keeppat: str = '*', varpat: str = '*', showvarpat: bool = True, exodif: any = <factory>, lwrun: bool = True, lwupdate: bool = False, lwreset: bool = True, lwsetbas: bool = True, outputwidget: str = 'jupviz', display_first: any = None, prefix_dict: dict = <factory>, vline: list = <factory>, relativ_start: int = 0, short: bool = False)[source]
Bases:
objectclass to input and run a model
display(wtotal) to activate the widget
- mmodel: any
- datawidget: any
- basename: str = 'Business as usual'
- keeppat: str = '*'
- varpat: str = '*'
- showvarpat: bool = True
- exodif: any
- lwrun: bool = True
- lwupdate: bool = False
- lwreset: bool = True
- lwsetbas: bool = True
- outputwidget: str = 'jupviz'
- display_first: any = None
- prefix_dict: dict
- vline: list
- relativ_start: int = 0
- short: bool = False
- class modelwidget_input.keep_plot_widget(mmodel: any, smpl: ~typing.Tuple[str, str] = ('', ''), relativ_start: int = 0, selected: str = '', selectfrom: str = '*', showselectfrom: bool = True, legend: bool = False, dec: str = '', use_descriptions: bool = True, select_width: str = '', select_height: str = '200px', vline: any = None, var_groups: dict = <factory>, use_var_groups: bool = True, add_var_name: bool = False, short: any = 0, select_scenario: bool = True, displaytype: str = 'tab', save_location: str = './graph', switch: bool = False, use_smpl: bool = False, init_dif: bool = False)[source]
Bases:
objectProvides an interactive widget to plot data from ModelFlow model instances. It allows for selection of variables, scenarios, and display types, and can show differences between scenarios in various formats.
- Parameters:
mmodel – A ModelFlow model instance.
smpl (Tuple[str, str], optional) – Sample period for plotting (start, end). Defaults to (‘’, ‘’).
use_smpl (bool, optional) – If True, enables a sample period selection slider. Defaults to False.
selectfrom (str, optional) – Space-separated string of variable names to select from. If empty, all kept variables are available. Defaults to ‘*’.
legend (bool, optional) – If True, displays legends next to the plots. Defaults to False.
dec (str, optional) – Format string for decimal places on the y-axis. Defaults to ‘’.
use_descriptions (bool, optional) – If True, uses variable descriptions from the model. Defaults to True.
vline (any, optional) – List of vertical lines for the plot (position, text). Defaults to None.
add_var_name (bool, optional) – If True, adds variable names to descriptions. Defaults to False.
short (any, optional) – If set, reduces the number of input fields. Defaults to 0.
select_scenario (bool, optional) – If True, allows selecting scenarios to display. Defaults to True.
switch (bool, optional) – If True, uses scenarios from mmodel.basedf and mmodel.lastdf. Defaults to False.
var_groups (dict, optional) – Dictionary of variable patterns for selection, like country groups. If empty use mmodel.var_groups. Defaults to an empty dict.
use_var_groups (bool, optional) – If True, enables selection using var_groups. Defaults to True.
displaytype (str, optional) – Type of display (‘tab’, ‘accordion’, or other). Defaults to ‘tab’.
save_location (str, optional) – Default location for saving plots. Defaults to ‘./graph’.
use_smpl – If True, enables a sample selection slider. Defaults to False.
- Properties:
show: Displays the widget. datawidget: The actual interactive widget.
- Returns:
An instance of keep_plot_widget. This instance’s ‘keep_wiz_figs’ property is set to a dictionary containing the figures, which can be used for creating publication-quality files.
- mmodel: any
- smpl: Tuple[str, str] = ('', '')
- relativ_start: int = 0
- selected: str = ''
- selectfrom: str = '*'
- showselectfrom: bool = True
- legend: bool = False
- dec: str = ''
- use_descriptions: bool = True
- select_width: str = ''
- select_height: str = '200px'
- vline: any = None
- var_groups: dict
- use_var_groups: bool = True
- add_var_name: bool = False
- short: any = 0
- select_scenario: bool = True
- displaytype: str = 'tab'
- save_location: str = './graph'
- switch: bool = False
- use_smpl: bool = False
- init_dif: bool = False
- property show
- class modelwidget_input.savefigs_widget(figs: dict = <factory>, location: str = './graph', addname: str = '')[source]
Bases:
objectProvides a widget for saving matplotlib figures from a dictionary to files.
The widget allows the user to specify the save location, file format(s), and additional naming details for the saved figures. It also includes an option to open the save location in the file explorer after saving.
- figs
A dictionary containing matplotlib figures. Defaults to an empty dict.
- Type:
dict, optional
- location
The default directory where figures will be saved. Defaults to ‘./graph’.
- Type:
str, optional
- addname
An additional suffix to append to the figure filenames. Defaults to an empty string.
- Type:
str, optional
- The widget layout includes:
A button to initiate the save process.
Input fields to specify the experiment name, save location, and filename suffix.
A multiple-selection dropdown to choose the file formats (e.g., svg, pdf, png, eps).
A checkbox to optionally open the save location after saving.
An output field displaying the final save location.
- figs: dict
- location: str = './graph'
- addname: str = ''