Skip to content

Commit

Permalink
Tweaks to SimpleOutput
Browse files Browse the repository at this point in the history
  • Loading branch information
Alan Fleming committed Dec 4, 2024
1 parent e6e674c commit 0b79975
Show file tree
Hide file tree
Showing 8 changed files with 99 additions and 68 deletions.
69 changes: 37 additions & 32 deletions examples/simple_output.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,28 @@
"cell_type": "markdown",
"id": "11",
"metadata": {},
"source": [
"### Other formats are also supported"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "12",
"metadata": {},
"outputs": [],
"source": [
"from IPython.display import HTML, Markdown\n",
"\n",
"so = SimpleOutput()\n",
"so.push(Markdown(\"## Markdown\"), HTML(\"<h2>HTML</h2>\"))\n",
"so"
]
},
{
"cell_type": "markdown",
"id": "13",
"metadata": {},
"source": [
"### set\n",
"\n",
Expand All @@ -131,7 +153,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "12",
"id": "14",
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -141,7 +163,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "13",
"id": "15",
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -151,7 +173,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "14",
"id": "16",
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -161,7 +183,7 @@
},
{
"cell_type": "markdown",
"id": "15",
"id": "17",
"metadata": {},
"source": [
"## max_continuous_streams\n",
Expand All @@ -176,7 +198,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "16",
"id": "18",
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -187,7 +209,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "17",
"id": "19",
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -197,7 +219,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "18",
"id": "20",
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -207,7 +229,7 @@
},
{
"cell_type": "markdown",
"id": "19",
"id": "21",
"metadata": {},
"source": [
"# AutoScroll\n",
Expand All @@ -221,7 +243,7 @@
},
{
"cell_type": "markdown",
"id": "20",
"id": "22",
"metadata": {},
"source": [
"## Builtin log viewer\n",
Expand All @@ -232,27 +254,18 @@
{
"cell_type": "code",
"execution_count": null,
"id": "21",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "22",
"id": "23",
"metadata": {},
"outputs": [],
"source": [
"app = ipylab.app\n",
"app.log_viewer.add_to_shell()\n",
"app.log_level = \"DEBUG\""
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "23",
"id": "24",
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -268,7 +281,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "24",
"id": "25",
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -277,7 +290,7 @@
},
{
"cell_type": "markdown",
"id": "25",
"id": "26",
"metadata": {},
"source": [
"## Example usage"
Expand All @@ -286,7 +299,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "26",
"id": "27",
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -301,7 +314,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "27",
"id": "28",
"metadata": {},
"outputs": [],
"source": [
Expand Down Expand Up @@ -358,14 +371,6 @@
")\n",
"p.add_to_shell(mode=ipylab.InsertMode.split_right)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "28",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
Expand Down
7 changes: 5 additions & 2 deletions ipylab/code_editor.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,17 @@
"text/x-python",
"text/x-ipython",
"text/x-markdown",
"application/json",
"text/html",
"text/css",
"text/csv",
"text/yaml",
"text/json",
"application/json",
)


@register
class CodeEditor(DOMWidget, Ipylab):
class CodeEditor(Ipylab, DOMWidget):
"""A Widget for code editing.
Code completion is provided for Python code for the specified namespace.
Expand Down
28 changes: 19 additions & 9 deletions ipylab/dialog.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,17 +65,28 @@ def show_dialog(
title: str = "",
body: str | Widget = "",
options: dict | None = None,
*,
has_close=True,
**kwgs: Unpack[IpylabKwgs],
) -> Task[dict[str, Any]]:
"""Jupyter dialog to get user response with custom buttons and checkbox.
returns {'value':any, 'isChecked':bool|None}
returns {'value':any, 'isChecked':bool|None}
'value' is the button 'accept' value of the selected button.
'value' is the button 'accept' value of the selected button.
title: 'Dialog title', // Can be text or a react element
body: 'Dialog body', // Can be text, a widget or a react element
specify kwgs passed as below.
title: str
The dialog title. // Can be text or a react element
body: str | Widget
Text to show in the body or a widget. 'Dialog body', // Can be text, a widget or a react element
has_close: bool [True]
By default (True), clicking outside the dialog will close it.
When `False`, the user must specifically cancel or accept a result.
options:
specify options can be passed as below.
buttons: [ // List of buttons
buttons=[
{
Expand Down Expand Up @@ -111,15 +122,14 @@ def show_dialog(
},
defaultButton: 0, // Index of the default button
focusNodeSelector: '.my-input', // Selector for focussing an input element when dialog opens
hasClose: false, // Whether to display a close button or not
renderer: undefined // To define customized dialog structure
see: https://jupyterlab.readthedocs.io/en/stable/api/functions/apputils.showDialog.html
source: https://jupyterlab.readthedocs.io/en/stable/extension/ui_helpers.html#generic-dialog
see: https://jupyterlab.readthedocs.io/en/stable/api/functions/apputils.showDialog.html
source: https://jupyterlab.readthedocs.io/en/stable/extension/ui_helpers.html#generic-dialog
"""
if isinstance(body, Widget) and "toLuminoWidget" not in kwgs:
kwgs["toLuminoWidget"] = ["body"]
return self.operation("showDialog", _combine(options, title=title, body=body), **kwgs)
return self.operation("showDialog", _combine(options, title=title, body=body, hasClose=has_close), **kwgs)

def show_error_message(
self, title: str, error: str, options: dict | None = None, **kwgs: Unpack[IpylabKwgs]
Expand Down
13 changes: 8 additions & 5 deletions ipylab/jupyterfrontend.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,19 @@


class LastUpdatedDict(OrderedDict):
"Store items in the order the keys were last added"
"""Store items in the order the keys were last added.
mode: Literal["first", "last"]
The end to shift the last added key."""

# ref: https://docs.python.org/3/library/collections.html#ordereddict-examples-and-recipes
_updating = False
_last = True

def __init__(self, *args, mode: Literal["first", "last"] = "last", **kwargs):
self._last = mode == "last"
super().__init__(*args, **kwargs)

def __setitem__(self, key, value):
super().__setitem__(key, value)
if not self._updating:
Expand All @@ -55,10 +62,6 @@ def update(self, m, **kwargs):
finally:
self._updating = False

def set_end(self, mode: Literal["first", "last"] = "last"):
"The end to move the last updated key."
self._last = mode == "last"


@register
class App(Ipylab):
Expand Down
5 changes: 3 additions & 2 deletions ipylab/log_viewer.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,9 @@ def close(self):

def _add_record(self, record: logging.LogRecord):
self._records.append(record)
self.output.push(record.output) # type: ignore
if record.levelno >= LogLevel.ERROR:
if self.output._ready: # noqa: SLF001
self.output.push(record.output) # type: ignore
if record.levelno >= LogLevel.ERROR and ipylab.app._ready: # noqa: SLF001
self._notify_exception(record)

def _notify_exception(self, record: logging.LogRecord):
Expand Down
2 changes: 1 addition & 1 deletion ipylab/menu.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ def activate(self):
return ipylab.app.commands.execute(f"{name}:open")


class MenuConnection(RankedMenu, InfoConnection):
class MenuConnection(InfoConnection, RankedMenu):
"""A connection to a custom menu"""

commands = Instance(CommandRegistry)
Expand Down
Loading

0 comments on commit 0b79975

Please sign in to comment.