From 9f3f0668ff8355db785e76331200ff1bce2cd912 Mon Sep 17 00:00:00 2001 From: Michael Scott Asato Cuthbert Date: Thu, 12 Jan 2023 15:28:07 -1000 Subject: [PATCH 1/9] Improve existing code --- .../developerReference/devTest_widgets.ipynb | 469 ++++++++++++++++++ music21/base.py | 3 + music21/common/misc.py | 2 +- music21/converter/subConverters.py | 34 +- music21/ipython21/objects.py | 4 +- 5 files changed, 495 insertions(+), 17 deletions(-) create mode 100644 documentation/source/developerReference/devTest_widgets.ipynb diff --git a/documentation/source/developerReference/devTest_widgets.ipynb b/documentation/source/developerReference/devTest_widgets.ipynb new file mode 100644 index 0000000000..1bcbe583c1 --- /dev/null +++ b/documentation/source/developerReference/devTest_widgets.ipynb @@ -0,0 +1,469 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 1, + "id": "1d93e3e7", + "metadata": {}, + "outputs": [], + "source": [ + "import ipywidgets" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "62d0119b", + "metadata": {}, + "outputs": [], + "source": [ + "import ipywidgets as widgets\n", + "from ipywidgets import HBox, VBox\n", + "import numpy as np\n", + "import matplotlib.pyplot as plt\n", + "from IPython.display import display\n", + "%matplotlib inline" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "9f8c6924", + "metadata": {}, + "outputs": [ + { + "data": { + "application/vnd.jupyter.widget-view+json": { + "model_id": "cbd6826489134266849b791336287317", + "version_major": 2, + "version_minor": 0 + }, + "text/plain": [ + "interactive(children=(IntSlider(value=5, description='x', max=15, min=-5), Output()), _dom_classes=('widget-in…" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "@widgets.interact\n", + "def f(x=5):\n", + " print(x)" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "ab4556b8", + "metadata": {}, + "outputs": [ + { + "data": { + "application/vnd.jupyter.widget-view+json": { + "model_id": "9e6ad1f6135a4015b8a407222e7af91a", + "version_major": 2, + "version_minor": 0 + }, + "text/plain": [ + "interactive(children=(IntSlider(value=5, description='x', max=5), Output()), _dom_classes=('widget-interact',)…" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "@widgets.interact(x=(0, 5))\n", + "def f(x=5):\n", + " print(x)" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "id": "703c72dc", + "metadata": {}, + "outputs": [ + { + "data": { + "application/vnd.jupyter.widget-view+json": { + "model_id": "90a0b8cbfd7d4136aad7bd0605616cac", + "version_major": 2, + "version_minor": 0 + }, + "text/plain": [ + "interactive(children=(FloatSlider(value=1.0, description='freq', max=3.0, min=-1.0), Dropdown(description='col…" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "@widgets.interact_manual(\n", + " color=['blue', 'red', 'green'], lw=(1., 10.))\n", + "def plot(freq=1., color='blue', lw=2, grid=True):\n", + " t = np.linspace(-1., +1., 1000)\n", + " fig, ax = plt.subplots(1, 1, figsize=(8, 6))\n", + " ax.plot(t, np.sin(2 * np.pi * freq * t),\n", + " lw=lw, color=color)\n", + " ax.grid(grid)" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "id": "cb9038af", + "metadata": {}, + "outputs": [ + { + "data": { + "application/vnd.jupyter.widget-view+json": { + "model_id": "2a3e6f4137bb44318a9bea3d0403cc58", + "version_major": 2, + "version_minor": 0 + }, + "text/plain": [ + "FloatSlider(value=2.0, description='Frequency:', max=10.0, min=1.0, readout_format='.1f')" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "freq_slider = widgets.FloatSlider(\n", + " value=2.,\n", + " min=1.,\n", + " max=10.0,\n", + " step=0.1,\n", + " description='Frequency:',\n", + " readout_format='.1f',\n", + ")\n", + "freq_slider" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "id": "32204e1f", + "metadata": {}, + "outputs": [], + "source": [ + "import ipywidgets as widgets\n", + "from traitlets import Unicode, Int, validate" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "id": "b94469d7", + "metadata": {}, + "outputs": [], + "source": [ + "class CounterWidget(widgets.DOMWidget):\n", + " _view_name = Unicode('CounterView').tag(sync=True)\n", + " _view_module = Unicode('counter').tag(sync=True)\n", + " value = Int(0).tag(sync=True)" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "id": "110723cf", + "metadata": {}, + "outputs": [ + { + "data": { + "application/javascript": [ + "// We make sure the `counter` module is defined\n", + "// only once.\n", + "require.undef('counter');\n", + "\n", + "// We define the `counter` module depending on the\n", + "// Jupyter widgets framework.\n", + "define('counter', [\"@jupyter-widgets/base\"],\n", + " function(widgets) {\n", + "\n", + " // We create the CounterView frontend class,\n", + " // deriving from DOMWidgetView.\n", + " var CounterView = widgets.DOMWidgetView.extend({\n", + "\n", + " // This method creates the HTML widget.\n", + " render: function() {\n", + " // The value_changed() method should be\n", + " // called when the model's value changes\n", + " // on the kernel side.\n", + " this.value_changed();\n", + " this.model.on('change:value',\n", + " this.value_changed, this);\n", + "\n", + " var model = this.model;\n", + " var that = this;\n", + "\n", + " // We create the plus and minus buttons.\n", + " this.bm = $('