Skip to content

Commit

Permalink
Reset base.py changes and make widget in another cell
Browse files Browse the repository at this point in the history
  • Loading branch information
atharva-2001 committed Nov 11, 2024
1 parent 090e64c commit 3ea1231
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 47 deletions.
8 changes: 1 addition & 7 deletions tardis/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ def run_tardis(
log_level=None,
specific_log_level=None,
show_progress_bars=True,
export_logger_widget=True,
**kwargs,
):
"""
Expand Down Expand Up @@ -90,7 +89,7 @@ def run_tardis(
if not isinstance(show_convergence_plots, bool):
raise TypeError("Expected bool in show_convergence_plots argument")

logger_widget = logging_state(log_level, tardis_config, specific_log_level)
logging_state(log_level, tardis_config, specific_log_level)

if atom_data is not None:
try:
Expand All @@ -115,9 +114,4 @@ def run_tardis(

simulation.run_convergence()
simulation.run_final()
simulation.logger_widget = logger_widget

if export_logger_widget == True:
display(logger_widget.embed())
logger_widget.visible = False
return simulation
75 changes: 35 additions & 40 deletions tardis/io/logger/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,36 @@

pn.extension()

def create_output_widget(height=300):
return pn.pane.HTML(
"",
height=height,
styles={
'overflow-y': 'auto',
'overflow-x': 'auto',
'border': '1px solid #ddd',
'width': '100%',
'font-family': 'monospace',
'padding': '8px',
'background-color': 'white'
}
)

log_outputs = {
"WARNING/ERROR": create_output_widget(),
"INFO": create_output_widget(),
"DEBUG": create_output_widget(),
"ALL": create_output_widget(),
}

tab_order = ["ALL", "WARNING/ERROR", "INFO", "DEBUG"]
tabs = pn.Tabs(
*[(title, log_outputs[title]) for title in tab_order],
height=350
)



@dataclass
class LoggingConfig:
LEVELS: dict[str, int] = field(default_factory=lambda: {
Expand Down Expand Up @@ -34,29 +64,8 @@ class LoggingConfig:
class TardisLogger:
def __init__(self):
self.config = LoggingConfig()
self.log_outputs = {
"WARNING/ERROR": self._create_output_widget(),
"INFO": self._create_output_widget(),
"DEBUG": self._create_output_widget(),
"ALL": self._create_output_widget(),
}
self.logger = logging.getLogger("tardis")

def _create_output_widget(self, height=300):
return pn.pane.HTML(
"",
height=height,
styles={
'overflow-y': 'auto',
'overflow-x': 'auto',
'border': '1px solid #ddd',
'width': '100%',
'font-family': 'monospace',
'padding': '8px',
'background-color': 'white'
}
)


def configure_logging(self, log_level, tardis_config, specific_log_level=None):
if "debug" in tardis_config:
specific_log_level = tardis_config["debug"].get(
Expand Down Expand Up @@ -106,24 +115,19 @@ def configure_logging(self, log_level, tardis_config, specific_log_level=None):
for logger in tardis_loggers:
logger.removeFilter(filter)


def setup_widget_logging(self):
"""Set up widget-based logging interface."""
widget_handler = LoggingHandler(self.log_outputs, self.config.COLORS)
widget_handler = LoggingHandler(log_outputs, self.config.COLORS)
widget_handler.setFormatter(
logging.Formatter("%(name)s [%(levelname)s] %(message)s (%(filename)s:%(lineno)d)")
)

self._configure_handlers(widget_handler)
tabs = self._create_and_display_tabs()


# self.periodic_cb = pn.state.add_periodic_callback(
# lambda: [output.param.trigger('object') for output in self.log_outputs.values()],
# period=100
# )

display(tabs)
return tabs
# )

def _configure_handlers(self, widget_handler):
"""Configure logging handlers."""
Expand All @@ -138,14 +142,6 @@ def _configure_handlers(self, widget_handler):
self.logger.addHandler(widget_handler)
logging.getLogger("py.warnings").addHandler(widget_handler)

def _create_and_display_tabs(self):
"""Create and display the logging tabs."""
tab_order = ["ALL", "WARNING/ERROR", "INFO", "DEBUG"]
tabs = pn.Tabs(
*[(title, self.log_outputs[title]) for title in tab_order],
height=350
)
return tabs


class LoggingHandler(logging.Handler):
Expand Down Expand Up @@ -221,5 +217,4 @@ def logging_state(log_level, tardis_config, specific_log_level=None):
"""Configure logging state for TARDIS."""
logger = TardisLogger()
logger.configure_logging(log_level, tardis_config, specific_log_level)
widget = logger.setup_widget_logging()
return widget
logger.setup_widget_logging()

0 comments on commit 3ea1231

Please sign in to comment.