diff --git a/examples/simple_output.ipynb b/examples/simple_output.ipynb index 8164844..5a759c9 100644 --- a/examples/simple_output.ipynb +++ b/examples/simple_output.ipynb @@ -90,23 +90,37 @@ "so" ] }, + { + "cell_type": "markdown", + "id": "8", + "metadata": {}, + "source": [ + "Or we could do it with one message..." + ] + }, { "cell_type": "code", "execution_count": null, - "id": "8", + "id": "9", "metadata": {}, "outputs": [], "source": [ - "# or we could do it with one message\n", - "so.clear()\n", - "so.push(*(f\"test {i}\\n\" for i in range(100)))\n", - "# Not that we've just sent 100 'outputs' in one message.\n", - "# All of the 100 lines are squashed into the 200px high simple output." + "so.clear(wait=False)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "10", + "metadata": {}, + "outputs": [], + "source": [ + "so.push(*(f\"test {i}\\n\" for i in range(50)))" ] }, { "cell_type": "markdown", - "id": "9", + "id": "11", "metadata": {}, "source": [ "### set\n", @@ -117,7 +131,7 @@ { "cell_type": "code", "execution_count": null, - "id": "10", + "id": "12", "metadata": {}, "outputs": [], "source": [ @@ -127,7 +141,7 @@ { "cell_type": "code", "execution_count": null, - "id": "11", + "id": "13", "metadata": {}, "outputs": [], "source": [ @@ -137,7 +151,7 @@ { "cell_type": "code", "execution_count": null, - "id": "12", + "id": "14", "metadata": {}, "outputs": [], "source": [ @@ -147,7 +161,7 @@ }, { "cell_type": "markdown", - "id": "13", + "id": "15", "metadata": {}, "source": [ "## max_continuous_streams\n", @@ -162,7 +176,7 @@ { "cell_type": "code", "execution_count": null, - "id": "14", + "id": "16", "metadata": {}, "outputs": [], "source": [ @@ -173,7 +187,7 @@ { "cell_type": "code", "execution_count": null, - "id": "15", + "id": "17", "metadata": {}, "outputs": [], "source": [ @@ -183,7 +197,7 @@ { "cell_type": "code", "execution_count": null, - "id": "16", + "id": "18", "metadata": {}, "outputs": [], "source": [ @@ -193,7 +207,7 @@ }, { "cell_type": "markdown", - "id": "17", + "id": "19", "metadata": {}, "source": [ "# AutoScroll\n", @@ -207,7 +221,7 @@ }, { "cell_type": "markdown", - "id": "18", + "id": "20", "metadata": {}, "source": [ "## Builtin log viewer\n", @@ -218,7 +232,7 @@ { "cell_type": "code", "execution_count": null, - "id": "19", + "id": "21", "metadata": {}, "outputs": [], "source": [ @@ -231,7 +245,7 @@ { "cell_type": "code", "execution_count": null, - "id": "20", + "id": "22", "metadata": {}, "outputs": [], "source": [ @@ -240,9 +254,19 @@ " app.log.error(\"Demo\")" ] }, + { + "cell_type": "code", + "execution_count": null, + "id": "23", + "metadata": {}, + "outputs": [], + "source": [ + "app.log_viewer.connections[0].close()" + ] + }, { "cell_type": "markdown", - "id": "21", + "id": "24", "metadata": {}, "source": [ "## Example usage" @@ -251,7 +275,7 @@ { "cell_type": "code", "execution_count": null, - "id": "22", + "id": "25", "metadata": {}, "outputs": [], "source": [ @@ -266,40 +290,61 @@ { "cell_type": "code", "execution_count": null, - "id": "23", + "id": "26", "metadata": {}, "outputs": [], "source": [ "vb = ipw.VBox()\n", "sw = AutoScroll(content=vb)\n", + "sw.sentinel_text = \"sentinel\"\n", "sw_holder = ipw.VBox([sw], layout={\"height\": \"200px\", \"border\": \"solid\"})\n", "\n", - "enabled = ipw.Checkbox(description=\"Auto scroll\", indent=False)\n", + "enabled = ipw.Checkbox(description=\"Auto scroll\", layout={\"width\": \"120px\"}, indent=False)\n", "ipw.link((sw, \"enabled\"), (enabled, \"value\"))\n", "sleep = ipw.FloatSlider(description=\"Sleep time (s)\", value=0.3, min=0.05, max=1, step=0.01)\n", "\n", - "b = ipw.Button(description=\"Start\")\n", + "b_start = ipw.Button(description=\"Start\", layout={\"width\": \"max-content\"})\n", + "b_clear = ipw.Button(description=\"Clear\", layout={\"width\": \"max-content\"})\n", + "direction = ipw.RadioButtons(options=[\"fwd\", \"rev\"], orientation=\"horizontal\", layout={\"width\": \"auto\"})\n", "\n", "\n", "def on_click(b):\n", - " if b.description == \"Start\":\n", - " import asyncio\n", + " if b is b_start:\n", + " if b.description == \"Start\":\n", + " import asyncio\n", + "\n", + " async def generate_output():\n", + " while True:\n", + " vb.children = (*vb.children, ipw.HTML(f\"It is now {datetime.now().isoformat()}\")) # noqa: DTZ005\n", + " await asyncio.sleep(sleep.value)\n", + "\n", + " b.task = ipylab.app.to_task(generate_output())\n", + " b.description = \"Stop\"\n", + " else:\n", + " b.task.cancel()\n", + " b.description = \"Start\"\n", + " if b is b_clear:\n", + " vb.children = ()\n", + "\n", + "\n", + "b_start.on_click(on_click)\n", + "b_clear.on_click(on_click)\n", "\n", - " async def generate_output():\n", - " while True:\n", - " vb.children = (*vb.children, ipw.HTML(f\"It is now {datetime.now().isoformat()}\")) # noqa: DTZ005\n", - " await asyncio.sleep(sleep.value)\n", "\n", - " b.task = ipylab.app.to_task(generate_output())\n", - " b.description = \"Stop\"\n", + "def _observe_direction(_):\n", + " if direction.value == \"fwd\":\n", + " sw.mode = \"end\"\n", + " vb.layout.flex_flow = \"column\"\n", " else:\n", - " b.task.cancel()\n", - " b.description = \"Start\"\n", + " sw.mode = \"start\"\n", + " vb.layout.flex_flow = \"column-reverse\"\n", "\n", "\n", - "b.on_click(on_click)\n", + "direction.observe(_observe_direction, \"value\")\n", "\n", - "p = ipylab.Panel([ipw.HBox([enabled, sleep, b], layout={\"justify_content\": \"center\"}), sw_holder])\n", + "p = ipylab.Panel(\n", + " [ipw.HBox([enabled, sleep, direction, b_start, b_clear], layout={\"justify_content\": \"center\"}), sw_holder]\n", + ")\n", "p.add_to_shell(mode=ipylab.InsertMode.split_right)" ] }