Skip to content

Commit

Permalink
Various tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
Alan Fleming committed Nov 22, 2024
1 parent 285c49d commit fa249c6
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 22 deletions.
2 changes: 1 addition & 1 deletion examples/plugins.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
19 changes: 15 additions & 4 deletions examples/simple_output.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@
"app = ipylab.app\n",
"\n",
"app.log_viewer.add_to_shell()\n",
"app.log_level = \"INFO\""
"app.log_level = \"DEBUG\""
]
},
{
Expand All @@ -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\")"
]
},
{
Expand Down Expand Up @@ -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": {
Expand Down
45 changes: 32 additions & 13 deletions ipylab/log.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
}


Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion ipylab/log_viewer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion ipylab/notification.py
Original file line number Diff line number Diff line change
Expand Up @@ -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())

Expand Down
6 changes: 4 additions & 2 deletions ipylab/simple_output.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]:
Expand Down

0 comments on commit fa249c6

Please sign in to comment.