Skip to content

Commit

Permalink
Add more features to simple_output.ipynb
Browse files Browse the repository at this point in the history
  • Loading branch information
Alan Fleming committed Nov 22, 2024
1 parent 312b338 commit 285c49d
Showing 1 changed file with 80 additions and 35 deletions.
115 changes: 80 additions & 35 deletions examples/simple_output.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -117,7 +131,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "10",
"id": "12",
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -127,7 +141,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "11",
"id": "13",
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -137,7 +151,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "12",
"id": "14",
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -147,7 +161,7 @@
},
{
"cell_type": "markdown",
"id": "13",
"id": "15",
"metadata": {},
"source": [
"## max_continuous_streams\n",
Expand All @@ -162,7 +176,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "14",
"id": "16",
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -173,7 +187,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "15",
"id": "17",
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -183,7 +197,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "16",
"id": "18",
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -193,7 +207,7 @@
},
{
"cell_type": "markdown",
"id": "17",
"id": "19",
"metadata": {},
"source": [
"# AutoScroll\n",
Expand All @@ -207,7 +221,7 @@
},
{
"cell_type": "markdown",
"id": "18",
"id": "20",
"metadata": {},
"source": [
"## Builtin log viewer\n",
Expand All @@ -218,7 +232,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "19",
"id": "21",
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -231,7 +245,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "20",
"id": "22",
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -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"
Expand All @@ -251,7 +275,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "22",
"id": "25",
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -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)"
]
}
Expand Down

0 comments on commit 285c49d

Please sign in to comment.