From fa249c6bf78842c5fa52f8b5882c86d3c616a6c5 Mon Sep 17 00:00:00 2001 From: Alan Fleming <> Date: Sat, 23 Nov 2024 10:22:51 +1100 Subject: [PATCH] Various tweaks --- examples/plugins.ipynb | 2 +- examples/simple_output.ipynb | 19 +++++++++++---- ipylab/log.py | 45 +++++++++++++++++++++++++----------- ipylab/log_viewer.py | 2 +- ipylab/notification.py | 2 +- ipylab/simple_output.py | 6 +++-- 6 files changed, 54 insertions(+), 22 deletions(-) diff --git a/examples/plugins.ipynb b/examples/plugins.ipynb index 9058dd4..31b38d1 100644 --- a/examples/plugins.ipynb +++ b/examples/plugins.ipynb @@ -136,7 +136,7 @@ " notify_message = ipw.Combobox(placeholder=\"Enter message\")\n", " notify_button = ipw.Button(description=\"Notify\")\n", " notify_button.on_click(lambda _: app.notification.notify(notify_message.value, notify_type.value)) # type: ignore\n", - " box = ipw.HBox([notify_type, notify_message, notify_button], layout={\"align_content\": \"center\"})\n", + " box = ipw.HBox([notify_type, notify_message, notify_button], layout={\"align_content\": \"center\", \"flex\": \"1 0 auto\"})\n", "\n", " out = ipw.Output()\n", " out.append_display_data(ipd.Markdown(md))\n", diff --git a/examples/simple_output.ipynb b/examples/simple_output.ipynb index 5a759c9..7c88ed9 100644 --- a/examples/simple_output.ipynb +++ b/examples/simple_output.ipynb @@ -239,7 +239,7 @@ "app = ipylab.app\n", "\n", "app.log_viewer.add_to_shell()\n", - "app.log_level = \"INFO\"" + "app.log_level = \"DEBUG\"" ] }, { @@ -249,9 +249,12 @@ "metadata": {}, "outputs": [], "source": [ - "for _ in range(100):\n", - " app.log.info(\"Demo\")\n", - " app.log.error(\"Demo\")" + "for _ in range(10):\n", + " app.log.debug(\"Debug\")\n", + " app.log.info(\"Info\")\n", + " app.log.error(\"Error\")\n", + " app.log.exception(\"Exception\")\n", + " app.log.critical(\"Critical\")" ] }, { @@ -347,6 +350,14 @@ ")\n", "p.add_to_shell(mode=ipylab.InsertMode.split_right)" ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "27", + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { diff --git a/ipylab/log.py b/ipylab/log.py index 1382e25..b1d261f 100644 --- a/ipylab/log.py +++ b/ipylab/log.py @@ -5,10 +5,9 @@ import logging import weakref -from enum import IntEnum +from enum import IntEnum, StrEnum from typing import TYPE_CHECKING, Any, ClassVar -from IPython.utils.coloransi import TermColors from ipywidgets import CallbackDispatcher import ipylab @@ -31,17 +30,37 @@ class LogLevel(IntEnum): DEBUG = 10 -grey = "\x1b[38;20m" -yellow = "\x1b[33;20m" -ret = "\x1b[31;20m" -bold_red = "\x1b[31;1m" -reset = TermColors.Normal +class ANSIColors(StrEnum): + # Jupyterlab supports ANSI colours + # https://jakob-bagterp.github.io/colorist-for-python/ansi-escape-codes/standard-16-colors/#structure + + reset = "\033[0m" + + black = "\033[30m" + red = "\033[31m" + green = "\033[32m" + yellow = "\033[33m" + blue = "\033[34m" + magenta = "\033[35m" + cyan = "\033[36m" + white = "\033[37m" + default = "\033[39m" + bright_black = "\033[90m" + bright_red = "\033[91m" + bright_green = "\033[92m" + bright_yellow = "\033[93m" + bright_blue = "\033[94m" + bright_magenta = "\033[95m" + bright_cyan = "\033[96m" + bright_white = "\033[97m" + + COLORS = { - LogLevel.DEBUG: TermColors.LightGray, - LogLevel.INFO: TermColors.Cyan, - LogLevel.WARNING: TermColors.LightRed, - LogLevel.ERROR: TermColors.Red, - LogLevel.CRITICAL: TermColors.Purple, + LogLevel.DEBUG: ANSIColors.cyan, + LogLevel.INFO: ANSIColors.bright_blue, + LogLevel.WARNING: ANSIColors.bright_yellow, + LogLevel.ERROR: ANSIColors.red, + LogLevel.CRITICAL: ANSIColors.bright_red, } @@ -110,7 +129,7 @@ def register_callback(self, callback, *, remove=False): class IpylabLogFormatter(logging.Formatter): - def __init__(self, *, colors: dict[LogLevel, str], reset=reset, **kwargs) -> None: + def __init__(self, *, colors: dict[LogLevel, str], reset=ANSIColors.reset, **kwargs) -> None: """Initialize the formatter with specified format strings.""" self.colors = COLORS | colors self.reset = reset diff --git a/ipylab/log_viewer.py b/ipylab/log_viewer.py index f776d1a..983f4ff 100644 --- a/ipylab/log_viewer.py +++ b/ipylab/log_viewer.py @@ -149,7 +149,7 @@ def observe(change: dict): await ipylab.app.dialog.show_dialog("Send record to console", body=body) if select.value: await ipylab.app.open_console( - objects={"record": select.value.record}, namespace_name=ipylab.app.active_namespace + objects={"record": select.value}, namespace_name=ipylab.app.active_namespace ) except Exception: return diff --git a/ipylab/notification.py b/ipylab/notification.py index 6a96dc6..576c39a 100644 --- a/ipylab/notification.py +++ b/ipylab/notification.py @@ -74,7 +74,7 @@ async def update(): to_object.extend(f"options.actions.{i}" for i in range(len(actions_))) for action in actions_: self.close_extras.add(action) - return await ipylab.app.notification.operation("update", toObject=to_object, args=args) + return await ipylab.app.notification.operation("update", {"args": args}, toObject=to_object) return self.to_task(update()) diff --git a/ipylab/simple_output.py b/ipylab/simple_output.py index c4c710b..c54d26a 100644 --- a/ipylab/simple_output.py +++ b/ipylab/simple_output.py @@ -73,8 +73,10 @@ def push(self, *outputs: dict[str, str] | Widget | str): if outputs_ := self._pack_outputs(outputs): self.send({"add": outputs_}) - def clear(self, *, wait=True): - "Clear the output" + def clear(self, *, wait=False): + """Clear the output. + wait: bool + True: Will delay clearing until next output is added.""" self.send({"clear": wait}) def set(self, *outputs: dict[str, str] | Widget | str, **kwgs: Unpack[IpylabKwgs]) -> Task[int]: