From 7936dbc71948286e7ea99a94649da8412e31ce28 Mon Sep 17 00:00:00 2001 From: martinRenou Date: Tue, 8 Mar 2022 11:44:05 +0100 Subject: [PATCH] Update docs --- docs/requirements.txt | 1 + docs/source/animation.ipynb | 122 ++++++ docs/source/clock.ipynb | 159 ++++++++ docs/source/conways_game_of_life.ipynb | 505 ++++++++++++++++++++++++ docs/source/drag_and_drop_example.ipynb | 154 ++++++++ docs/source/fractals_tree.ipynb | 212 ++++++++++ docs/source/ipycanvas.ipynb | 25 +- docs/source/jupyterlite_config.json | 5 +- 8 files changed, 1170 insertions(+), 13 deletions(-) create mode 100644 docs/source/animation.ipynb create mode 100644 docs/source/clock.ipynb create mode 100644 docs/source/conways_game_of_life.ipynb create mode 100644 docs/source/drag_and_drop_example.ipynb create mode 100644 docs/source/fractals_tree.ipynb diff --git a/docs/requirements.txt b/docs/requirements.txt index ff47194..056d909 100644 --- a/docs/requirements.txt +++ b/docs/requirements.txt @@ -1,3 +1,4 @@ ipycanvas +sphinx===4.4.0 jupyterlite-sphinx pydata-sphinx-theme diff --git a/docs/source/animation.ipynb b/docs/source/animation.ipynb new file mode 100644 index 0000000..d857aeb --- /dev/null +++ b/docs/source/animation.ipynb @@ -0,0 +1,122 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "import micropip\n", + "\n", + "await micropip.install(\"ipycanvas\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "import asyncio\n", + "\n", + "from ipycanvas import Canvas, hold_canvas" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "import numpy as np" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "from math import pi, cos, sin" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "def fast_draw(canvas, t):\n", + " \"\"\"Same as draw, but using NumPy and the vectorized version of fill_circle: fill_circles\"\"\"\n", + " size = 1000\n", + " step = 20\n", + " t1 = t / 1000.0\n", + "\n", + " x = np.linspace(0, size, int(size / step))\n", + " y = np.linspace(0, size, int(size / step))\n", + " xv, yv = np.meshgrid(x, y)\n", + "\n", + " x_angle = y_angle = 2 * pi\n", + "\n", + " angle = x_angle * (xv / size) + y_angle * (yv / size)\n", + "\n", + " particle_x = xv + 20 * np.cos(2 * pi * t1 + angle)\n", + " particle_y = yv + 20 * np.sin(2 * pi * t1 + angle)\n", + "\n", + " canvas.fill_circles(particle_x, particle_y, 6)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "size = 1000\n", + "canvas = Canvas(width=size, height=size)\n", + "canvas" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "from time import sleep\n", + "\n", + "for i in range(200):\n", + " with hold_canvas(canvas):\n", + " canvas.clear()\n", + " canvas.fill_style = \"white\"\n", + " canvas.fill_rect(0, 0, size, size)\n", + " canvas.fill_style = \"#fcba03\"\n", + "\n", + " fast_draw(canvas, i * 20.0)\n", + "\n", + " await asyncio.sleep(20 / 1000.0)" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.9.5" + } + }, + "nbformat": 4, + "nbformat_minor": 4 +} diff --git a/docs/source/clock.ipynb b/docs/source/clock.ipynb new file mode 100644 index 0000000..7c56bee --- /dev/null +++ b/docs/source/clock.ipynb @@ -0,0 +1,159 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "import micropip\n", + "\n", + "await micropip.install(\"ipycanvas\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "from ipycanvas import Canvas, hold_canvas\n", + "import numpy as np\n", + "\n", + "CLOCK_RADIUS = 100\n", + "\n", + "canvas = Canvas(width=CLOCK_RADIUS * 2.5, height=CLOCK_RADIUS * 2.5)\n", + "canvas.translate(CLOCK_RADIUS * 1.2, CLOCK_RADIUS * 1.2)\n", + "\n", + "\n", + "def clear_drawing():\n", + " canvas.clear_rect(\n", + " -CLOCK_RADIUS * 1.2, -CLOCK_RADIUS * 1.2, canvas.width, canvas.height\n", + " )\n", + "\n", + "\n", + "def minutes_vec(minutes):\n", + " a = minutes * np.pi / 30\n", + " return [np.sin(a), -np.cos(a)]\n", + "\n", + "\n", + "def draw_dial():\n", + " ht = 10\n", + " mt = 6\n", + " ho = 20\n", + " mo = 10\n", + "\n", + " canvas.text_align = \"center\"\n", + " canvas.text_baseline = \"middle\"\n", + "\n", + " canvas.line_width = 2\n", + " canvas.stroke_circle(0, 0, CLOCK_RADIUS)\n", + "\n", + " for m in range(60):\n", + " a = m * np.pi / 30\n", + " x, y = np.sin(a), -np.cos(a)\n", + "\n", + " canvas.line_width = 1\n", + " if m % 5 == 0:\n", + " canvas.stroke_line(\n", + " x * (CLOCK_RADIUS - ht),\n", + " y * (CLOCK_RADIUS - ht),\n", + " x * CLOCK_RADIUS,\n", + " y * CLOCK_RADIUS,\n", + " )\n", + " canvas.font = \"12px serif\"\n", + " canvas.stroke_text(str(m), x * (CLOCK_RADIUS + mo), y * (CLOCK_RADIUS + mo))\n", + " canvas.font = \"16px serif\"\n", + " canvas.stroke_text(\n", + " str(m // 5 if m > 0 else 12),\n", + " x * (CLOCK_RADIUS - ho),\n", + " y * (CLOCK_RADIUS - ho),\n", + " )\n", + " else:\n", + " canvas.stroke_line(\n", + " x * (CLOCK_RADIUS - mt),\n", + " y * (CLOCK_RADIUS - mt),\n", + " x * CLOCK_RADIUS,\n", + " y * CLOCK_RADIUS,\n", + " )\n", + "\n", + "\n", + "def draw_hands(minutes):\n", + " ms = 35\n", + " hs = 50\n", + "\n", + " hrs = minutes // 60\n", + " mins = minutes % 60\n", + "\n", + " mv = minutes_vec(mins)\n", + " hv = minutes_vec(hrs * 5 + (mins / 12))\n", + "\n", + " canvas.line_width = 5\n", + " canvas.stroke_line(0, 0, mv[0] * (CLOCK_RADIUS - ms), mv[1] * (CLOCK_RADIUS - ms))\n", + " canvas.stroke_line(0, 0, hv[0] * (CLOCK_RADIUS - hs), hv[1] * (CLOCK_RADIUS - hs))\n", + "\n", + "\n", + "def draw_clock(hours, minutes):\n", + " with hold_canvas(canvas):\n", + " clear_drawing()\n", + " draw_dial()\n", + " draw_hands((hours % 12) * 60 + minutes)\n", + "\n", + "\n", + "canvas" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "import datetime\n", + "import ipywidgets as widgets\n", + "\n", + "now = datetime.datetime.now()\n", + "\n", + "hour_text = widgets.IntText(\n", + " value=now.hour, continuous_update=True, layout={\"width\": \"50px\"}\n", + ")\n", + "minute_text = widgets.IntText(\n", + " value=now.minute, continuous_update=True, layout={\"width\": \"50px\"}\n", + ")\n", + "\n", + "\n", + "def on_text_change(change):\n", + " draw_clock(int(hour_text.value), int(minute_text.value))\n", + "\n", + "\n", + "hour_text.observe(on_text_change, names=\"value\")\n", + "minute_text.observe(on_text_change, names=\"value\")\n", + "\n", + "on_text_change(0)\n", + "\n", + "widgets.HBox([hour_text, widgets.Label(value=\":\"), minute_text])" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.9.5" + } + }, + "nbformat": 4, + "nbformat_minor": 4 +} diff --git a/docs/source/conways_game_of_life.ipynb b/docs/source/conways_game_of_life.ipynb new file mode 100644 index 0000000..180e500 --- /dev/null +++ b/docs/source/conways_game_of_life.ipynb @@ -0,0 +1,505 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# ipycanvas: John Conway's Game Of Life" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Some of the following code is adapted from https://jakevdp.github.io/blog/2013/08/07/conways-game-of-life/" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "import micropip\n", + "\n", + "await micropip.install(\"ipycanvas\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "import asyncio\n", + "\n", + "import numpy as np\n", + "\n", + "from ipycanvas import RoughCanvas, hold_canvas" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "def life_step(x):\n", + " \"\"\"Game of life step\"\"\"\n", + " nbrs_count = sum(\n", + " np.roll(np.roll(x, i, 0), j, 1)\n", + " for i in (-1, 0, 1)\n", + " for j in (-1, 0, 1)\n", + " if (i != 0 or j != 0)\n", + " )\n", + " return (nbrs_count == 3) | (x & (nbrs_count == 2))" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "def draw(x, canvas, color=\"black\"):\n", + " with hold_canvas(canvas):\n", + " canvas.clear()\n", + " canvas.fill_style = \"#FFF0C9\"\n", + " canvas.stroke_style = \"white\"\n", + " canvas.fill_rect(0, 0, canvas.size[0], canvas.size[1])\n", + "\n", + " canvas.fill_style = color\n", + " canvas.stroke_style = color\n", + "\n", + " living_cells = np.where(x)\n", + "\n", + " rects_x = living_cells[0] * n_pixels\n", + " rects_y = living_cells[1] * n_pixels\n", + "\n", + " canvas.fill_rects(rects_x, rects_y, n_pixels)\n", + " canvas.stroke_rects(rects_x, rects_y, n_pixels)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "glider_gun = [\n", + " [\n", + " 0,\n", + " 0,\n", + " 0,\n", + " 0,\n", + " 0,\n", + " 0,\n", + " 0,\n", + " 0,\n", + " 0,\n", + " 0,\n", + " 0,\n", + " 0,\n", + " 0,\n", + " 0,\n", + " 0,\n", + " 0,\n", + " 0,\n", + " 0,\n", + " 0,\n", + " 0,\n", + " 0,\n", + " 0,\n", + " 0,\n", + " 0,\n", + " 1,\n", + " 0,\n", + " 0,\n", + " 0,\n", + " 0,\n", + " 0,\n", + " 0,\n", + " 0,\n", + " 0,\n", + " 0,\n", + " 0,\n", + " 0,\n", + " ],\n", + " [\n", + " 0,\n", + " 0,\n", + " 0,\n", + " 0,\n", + " 0,\n", + " 0,\n", + " 0,\n", + " 0,\n", + " 0,\n", + " 0,\n", + " 0,\n", + " 0,\n", + " 0,\n", + " 0,\n", + " 0,\n", + " 0,\n", + " 0,\n", + " 0,\n", + " 0,\n", + " 0,\n", + " 0,\n", + " 0,\n", + " 1,\n", + " 0,\n", + " 1,\n", + " 0,\n", + " 0,\n", + " 0,\n", + " 0,\n", + " 0,\n", + " 0,\n", + " 0,\n", + " 0,\n", + " 0,\n", + " 0,\n", + " 0,\n", + " ],\n", + " [\n", + " 0,\n", + " 0,\n", + " 0,\n", + " 0,\n", + " 0,\n", + " 0,\n", + " 0,\n", + " 0,\n", + " 0,\n", + " 0,\n", + " 0,\n", + " 0,\n", + " 1,\n", + " 1,\n", + " 0,\n", + " 0,\n", + " 0,\n", + " 0,\n", + " 0,\n", + " 0,\n", + " 1,\n", + " 1,\n", + " 0,\n", + " 0,\n", + " 0,\n", + " 0,\n", + " 0,\n", + " 0,\n", + " 0,\n", + " 0,\n", + " 0,\n", + " 0,\n", + " 0,\n", + " 0,\n", + " 1,\n", + " 1,\n", + " ],\n", + " [\n", + " 0,\n", + " 0,\n", + " 0,\n", + " 0,\n", + " 0,\n", + " 0,\n", + " 0,\n", + " 0,\n", + " 0,\n", + " 0,\n", + " 0,\n", + " 1,\n", + " 0,\n", + " 0,\n", + " 0,\n", + " 1,\n", + " 0,\n", + " 0,\n", + " 0,\n", + " 0,\n", + " 1,\n", + " 1,\n", + " 0,\n", + " 0,\n", + " 0,\n", + " 0,\n", + " 0,\n", + " 0,\n", + " 0,\n", + " 0,\n", + " 0,\n", + " 0,\n", + " 0,\n", + " 0,\n", + " 1,\n", + " 1,\n", + " ],\n", + " [\n", + " 1,\n", + " 1,\n", + " 0,\n", + " 0,\n", + " 0,\n", + " 0,\n", + " 0,\n", + " 0,\n", + " 0,\n", + " 0,\n", + " 1,\n", + " 0,\n", + " 0,\n", + " 0,\n", + " 0,\n", + " 0,\n", + " 1,\n", + " 0,\n", + " 0,\n", + " 0,\n", + " 1,\n", + " 1,\n", + " 0,\n", + " 0,\n", + " 0,\n", + " 0,\n", + " 0,\n", + " 0,\n", + " 0,\n", + " 0,\n", + " 0,\n", + " 0,\n", + " 0,\n", + " 0,\n", + " 0,\n", + " 0,\n", + " ],\n", + " [\n", + " 1,\n", + " 1,\n", + " 0,\n", + " 0,\n", + " 0,\n", + " 0,\n", + " 0,\n", + " 0,\n", + " 0,\n", + " 0,\n", + " 1,\n", + " 0,\n", + " 0,\n", + " 0,\n", + " 1,\n", + " 0,\n", + " 1,\n", + " 1,\n", + " 0,\n", + " 0,\n", + " 0,\n", + " 0,\n", + " 1,\n", + " 0,\n", + " 1,\n", + " 0,\n", + " 0,\n", + " 0,\n", + " 0,\n", + " 0,\n", + " 0,\n", + " 0,\n", + " 0,\n", + " 0,\n", + " 0,\n", + " 0,\n", + " ],\n", + " [\n", + " 0,\n", + " 0,\n", + " 0,\n", + " 0,\n", + " 0,\n", + " 0,\n", + " 0,\n", + " 0,\n", + " 0,\n", + " 0,\n", + " 1,\n", + " 0,\n", + " 0,\n", + " 0,\n", + " 0,\n", + " 0,\n", + " 1,\n", + " 0,\n", + " 0,\n", + " 0,\n", + " 0,\n", + " 0,\n", + " 0,\n", + " 0,\n", + " 1,\n", + " 0,\n", + " 0,\n", + " 0,\n", + " 0,\n", + " 0,\n", + " 0,\n", + " 0,\n", + " 0,\n", + " 0,\n", + " 0,\n", + " 0,\n", + " ],\n", + " [\n", + " 0,\n", + " 0,\n", + " 0,\n", + " 0,\n", + " 0,\n", + " 0,\n", + " 0,\n", + " 0,\n", + " 0,\n", + " 0,\n", + " 0,\n", + " 1,\n", + " 0,\n", + " 0,\n", + " 0,\n", + " 1,\n", + " 0,\n", + " 0,\n", + " 0,\n", + " 0,\n", + " 0,\n", + " 0,\n", + " 0,\n", + " 0,\n", + " 0,\n", + " 0,\n", + " 0,\n", + " 0,\n", + " 0,\n", + " 0,\n", + " 0,\n", + " 0,\n", + " 0,\n", + " 0,\n", + " 0,\n", + " 0,\n", + " ],\n", + " [\n", + " 0,\n", + " 0,\n", + " 0,\n", + " 0,\n", + " 0,\n", + " 0,\n", + " 0,\n", + " 0,\n", + " 0,\n", + " 0,\n", + " 0,\n", + " 0,\n", + " 1,\n", + " 1,\n", + " 0,\n", + " 0,\n", + " 0,\n", + " 0,\n", + " 0,\n", + " 0,\n", + " 0,\n", + " 0,\n", + " 0,\n", + " 0,\n", + " 0,\n", + " 0,\n", + " 0,\n", + " 0,\n", + " 0,\n", + " 0,\n", + " 0,\n", + " 0,\n", + " 0,\n", + " 0,\n", + " 0,\n", + " 0,\n", + " ],\n", + "]\n", + "\n", + "x = np.zeros((50, 70), dtype=bool)\n", + "x[1:10, 1:37] = glider_gun" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "n_pixels = 15\n", + "\n", + "canvas = RoughCanvas(width=x.shape[0] * n_pixels, height=x.shape[1] * n_pixels)\n", + "canvas.fill_style = \"#FFF0C9\"\n", + "canvas.stroke_style = \"white\"\n", + "canvas.fill_rect(0, 0, canvas.size[0], canvas.size[1])\n", + "\n", + "canvas" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "draw(x, canvas, \"#5770B3\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "for _ in range(1_000):\n", + " x = life_step(x)\n", + " draw(x, canvas, \"#5770B3\")\n", + "\n", + " await asyncio.sleep(0.1)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.8.10" + } + }, + "nbformat": 4, + "nbformat_minor": 4 +} diff --git a/docs/source/drag_and_drop_example.ipynb b/docs/source/drag_and_drop_example.ipynb new file mode 100644 index 0000000..925334d --- /dev/null +++ b/docs/source/drag_and_drop_example.ipynb @@ -0,0 +1,154 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "import micropip\n", + "\n", + "await micropip.install(\"ipycanvas\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "from ipycanvas import Canvas, hold_canvas\n", + "\n", + "canvas = Canvas(width=600, height=600)\n", + "canvas.fill_style = \"#584f4e\"\n", + "canvas.fill_rect(0, 0, 600, 600)\n", + "\n", + "objects_to_draw = []\n", + "\n", + "\n", + "class Square_obj:\n", + " def __init__(self, x, y, width=100, height=40):\n", + " self.x = x\n", + " self.y = y\n", + " self.width = width\n", + " self.height = height\n", + " self.selected = False\n", + " objects_to_draw.append(self)\n", + "\n", + " def set_x_y(self, x_in, y_in):\n", + " self.x = x_in\n", + " self.y = y_in\n", + "\n", + " def draw(self):\n", + "\n", + " canvas.fill_style = \"#38a8a4\"\n", + " canvas.fill_rect(\n", + " self.x - (self.width * 0.5), self.y - (self.height), self.width, self.height\n", + " )\n", + " if self.selected:\n", + " canvas.fill_style = \"#9dcea6\"\n", + " else:\n", + " canvas.fill_style = \"#dee7bc\"\n", + " canvas.fill_rect(\n", + " self.x - (self.width * 0.5),\n", + " self.y - (self.height * 0.5),\n", + " self.width,\n", + " self.height,\n", + " )\n", + "\n", + " def is_selected(self, x_in, y_in):\n", + " x_coord = self.x - (self.width * 0.5)\n", + " y_coord = self.y - (self.height * 0.5)\n", + "\n", + " if (\n", + " x_in > x_coord\n", + " and x_in < (x_coord + self.width)\n", + " and y_in > y_coord\n", + " and y_in < (y_coord + self.height)\n", + " ):\n", + "\n", + " self.set_selected(True)\n", + " return True\n", + " else:\n", + " self.set_selected(False)\n", + " return False\n", + "\n", + " def set_selected(self, state):\n", + " self.selected = state\n", + "\n", + "\n", + "def canvas_restart():\n", + " canvas.clear()\n", + " canvas.fill_style = \"#584f4e\"\n", + " canvas.fill_rect(0, 0, 600, 600)\n", + "\n", + "\n", + "def handle_mouse_down(x, y):\n", + " if [o for o in objects_to_draw if o.selected]:\n", + " [o.set_selected(False) for o in objects_to_draw if o.selected]\n", + " return False\n", + "\n", + " check_bool_pos = list(\n", + " set([check_region.is_selected(x, y) for check_region in objects_to_draw])\n", + " )\n", + " if len(check_bool_pos) == 1:\n", + " if check_bool_pos[0] == False:\n", + " s = Square_obj(x, y)\n", + " s.set_selected(False)\n", + " s.draw()\n", + "\n", + " else:\n", + " canvas_restart()\n", + " [o.draw() for o in objects_to_draw]\n", + "\n", + " if len(check_bool_pos) == 0:\n", + " s = Square_obj(x, y)\n", + " s.set_selected(False)\n", + " s.draw()\n", + "\n", + "\n", + "def handle_mouse_move(x, y):\n", + " if [o for o in objects_to_draw if o.selected]:\n", + " with hold_canvas(canvas):\n", + " [o for o in objects_to_draw if o.selected][-1].set_x_y(x, y)\n", + " canvas_restart()\n", + " [o.draw() for o in objects_to_draw]\n", + "\n", + "\n", + "canvas.on_mouse_down(handle_mouse_down)\n", + "canvas.on_mouse_move(handle_mouse_move)\n", + "\n", + "\n", + "canvas" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.9.5" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/docs/source/fractals_tree.ipynb b/docs/source/fractals_tree.ipynb new file mode 100644 index 0000000..3f18e20 --- /dev/null +++ b/docs/source/fractals_tree.ipynb @@ -0,0 +1,212 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "import micropip\n", + "\n", + "await micropip.install(\"ipycanvas\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "from math import pi\n", + "from random import uniform\n", + "\n", + "from ipywidgets import Button\n", + "\n", + "from ipycanvas import Canvas, hold_canvas" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "canvas = Canvas(width=800, height=600)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "def recursive_draw_leaf(canvas, length, r_angle, r_factor, l_angle, l_factor):\n", + " canvas.stroke_line(0, 0, 0, -length)\n", + " canvas.translate(0, -length)\n", + "\n", + " if length > 5:\n", + " canvas.save()\n", + "\n", + " canvas.rotate(r_angle)\n", + " recursive_draw_leaf(\n", + " canvas, length * r_factor, r_angle, r_factor, l_angle, l_factor\n", + " )\n", + "\n", + " canvas.restore()\n", + "\n", + " canvas.save()\n", + "\n", + " canvas.rotate(l_angle)\n", + " recursive_draw_leaf(\n", + " canvas, length * l_factor, r_angle, r_factor, l_angle, l_factor\n", + " )\n", + "\n", + " canvas.restore()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "def draw_tree(canvas):\n", + " with hold_canvas(canvas):\n", + " canvas.save()\n", + "\n", + " canvas.clear()\n", + "\n", + " canvas.translate(canvas.width / 2.0, canvas.height)\n", + "\n", + " canvas.stroke_style = \"black\"\n", + "\n", + " r_factor = uniform(0.6, 0.8)\n", + " l_factor = uniform(0.6, 0.8)\n", + "\n", + " r_angle = uniform(pi / 10.0, pi / 5.0)\n", + " l_angle = uniform(-pi / 5.0, -pi / 10.0)\n", + "\n", + " recursive_draw_leaf(canvas, 150, r_angle, r_factor, l_angle, l_factor)\n", + "\n", + " canvas.restore()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "button = Button(description=\"Generate tree!\")\n", + "\n", + "\n", + "def click_callback(*args, **kwargs):\n", + " global canvas\n", + "\n", + " draw_tree(canvas)\n", + "\n", + "\n", + "button.on_click(click_callback)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "draw_tree(canvas)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "canvas" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "button" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Acknowledgment:\n", + "\n", + "This Notebook is adapted from a Notebook by [Eric MADEC](https://github.com/ericecmorlaix) which was itself adapted from https://medium.com/better-programming/learning-p5-js-by-making-fractals-cbdcac5c651e" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.9.5" + }, + "toc": { + "base_numbering": 1, + "nav_menu": {}, + "number_sections": true, + "sideBar": true, + "skip_h1_title": false, + "title_cell": "Table of Contents", + "title_sidebar": "Contents", + "toc_cell": false, + "toc_position": {}, + "toc_section_display": true, + "toc_window_display": false + }, + "varInspector": { + "cols": { + "lenName": 16, + "lenType": 16, + "lenVar": 40 + }, + "kernels_config": { + "python": { + "delete_cmd_postfix": "", + "delete_cmd_prefix": "del ", + "library": "var_list.py", + "varRefreshCmd": "print(var_dic_list())" + }, + "r": { + "delete_cmd_postfix": ") ", + "delete_cmd_prefix": "rm(", + "library": "var_list.r", + "varRefreshCmd": "cat(var_dic_list()) " + } + }, + "types_to_exclude": [ + "module", + "function", + "builtin_function_or_method", + "instance", + "_Feature" + ], + "window_display": false + } + }, + "nbformat": 4, + "nbformat_minor": 4 +} diff --git a/docs/source/ipycanvas.ipynb b/docs/source/ipycanvas.ipynb index 59e7941..4bcc435 100644 --- a/docs/source/ipycanvas.ipynb +++ b/docs/source/ipycanvas.ipynb @@ -10,7 +10,8 @@ "# This cell is only needed in this documentation page\n", "# do not try to run this in your Notebooks if not using JupyterLite, it won't work :)\n", "import piplite\n", - "await piplite.install('ipycanvas')" + "\n", + "await piplite.install(\"ipycanvas\")" ] }, { @@ -24,22 +25,24 @@ "\n", "from ipycanvas import Canvas\n", "\n", - "canvas = Canvas(width=1600, height=1200, layout=dict(width='100%'))\n", + "canvas = Canvas(width=1600, height=1200, layout=dict(width=\"100%\"))\n", "\n", - "canvas.fill_style = '#8ee05e'\n", + "canvas.fill_style = \"#8ee05e\"\n", "canvas.fill_rect(0, 0, canvas.width, canvas.height)\n", "\n", - "canvas.fill_style = '#f5f533'\n", - "canvas.fill_circle(canvas.width / 2., canvas.height / 2., 500)\n", + "canvas.fill_style = \"#f5f533\"\n", + "canvas.fill_circle(canvas.width / 2.0, canvas.height / 2.0, 500)\n", "\n", - "canvas.stroke_style = 'black'\n", + "canvas.stroke_style = \"black\"\n", "canvas.line_width = 30\n", - "canvas.stroke_circle(canvas.width / 2., canvas.height / 2., 500)\n", + "canvas.stroke_circle(canvas.width / 2.0, canvas.height / 2.0, 500)\n", "\n", - "canvas.fill_style = 'black'\n", - "canvas.fill_circle(canvas.width / 2.7, canvas.height / 3., 100) # Right eye\n", - "canvas.stroke_arc(canvas.width / 2., canvas.height / 2., 400, 0, pi, False) # Mouth\n", - "canvas.stroke_arc(canvas.width - canvas.width / 2.7, canvas.height / 2.7, 100, 0, pi, True) # Left eye\n", + "canvas.fill_style = \"black\"\n", + "canvas.fill_circle(canvas.width / 2.7, canvas.height / 3.0, 100) # Right eye\n", + "canvas.stroke_arc(canvas.width / 2.0, canvas.height / 2.0, 400, 0, pi, False) # Mouth\n", + "canvas.stroke_arc(\n", + " canvas.width - canvas.width / 2.7, canvas.height / 2.7, 100, 0, pi, True\n", + ") # Left eye\n", "\n", "canvas" ] diff --git a/docs/source/jupyterlite_config.json b/docs/source/jupyterlite_config.json index bfcca82..28611ec 100644 --- a/docs/source/jupyterlite_config.json +++ b/docs/source/jupyterlite_config.json @@ -1,7 +1,8 @@ { "LiteBuildConfig": { "federated_extensions": [ - "https://github.com/conda-forge/releases/releases/download/noarch/ipycanvas-0.10.2-pyhd8ed1ab_0.tar.bz2/ipycanvas-0.10.2-pyhd8ed1ab_0.tar.bz2", + "https://github.com/conda-forge/releases/releases/download/noarch/jupyterlab_widgets-1.0.2-pyhd8ed1ab_0.tar.bz2/jupyterlab_widgets-1.0.2-pyhd8ed1ab_0.tar.bz2", + "https://github.com/conda-forge/releases/releases/download/noarch/ipycanvas-0.10.2-pyhd8ed1ab_0.tar.bz2/ipycanvas-0.10.2-pyhd8ed1ab_0.tar.bz2" ], "ignore_sys_prefix": true, "piplite_urls": [ @@ -18,7 +19,7 @@ "https://files.pythonhosted.org/packages/py3/j/jupyterlab-widgets/jupyterlab_widgets-1.0.2-py3-none-any.whl", "https://files.pythonhosted.org/packages/py3/n/nest-asyncio/nest_asyncio-1.5.4-py3-none-any.whl", "https://files.pythonhosted.org/packages/py3/n/notebook/notebook-6.4.8-py3-none-any.whl", - "https://files.pythonhosted.org/packages/py3/t/traitlets/traitlets-5.1.1-py3-none-any.whl", + "https://files.pythonhosted.org/packages/py3/t/traitlets/traitlets-5.1.1-py3-none-any.whl" ] } }