├── README.md └── node-example-latest.ipynb /README.md: -------------------------------------------------------------------------------- 1 | # notes -------------------------------------------------------------------------------- /node-example-latest.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 9, 6 | "metadata": { 7 | "scrolled": true, 8 | "tags": [ 9 | "hide-output", 10 | "hide-cell" 11 | ] 12 | }, 13 | "outputs": [ 14 | { 15 | "name": "stdout", 16 | "output_type": "stream", 17 | "text": [ 18 | "Requirement already satisfied: pixiedust in /opt/conda/lib/python3.7/site-packages (1.1.18)\n", 19 | "Requirement already satisfied: pixiedust_node in /opt/conda/lib/python3.7/site-packages (0.2.5)\n", 20 | "Requirement already satisfied: mpld3 in /opt/conda/lib/python3.7/site-packages (from pixiedust) (0.5.1)\n", 21 | "Requirement already satisfied: geojson in /opt/conda/lib/python3.7/site-packages (from pixiedust) (2.5.0)\n", 22 | "Requirement already satisfied: markdown in /opt/conda/lib/python3.7/site-packages (from pixiedust) (3.3.3)\n", 23 | "Requirement already satisfied: colour in /opt/conda/lib/python3.7/site-packages (from pixiedust) (0.1.5)\n", 24 | "Requirement already satisfied: lxml in /opt/conda/lib/python3.7/site-packages (from pixiedust) (4.6.1)\n", 25 | "Requirement already satisfied: requests in /opt/conda/lib/python3.7/site-packages (from pixiedust) (2.22.0)\n", 26 | "Requirement already satisfied: astunparse in /opt/conda/lib/python3.7/site-packages (from pixiedust) (1.6.3)\n", 27 | "Requirement already satisfied: ipython in /opt/conda/lib/python3.7/site-packages (from pixiedust_node) (7.11.1)\n", 28 | "Requirement already satisfied: pandas in /opt/conda/lib/python3.7/site-packages (from pixiedust_node) (1.1.4)\n", 29 | "Requirement already satisfied: matplotlib in /opt/conda/lib/python3.7/site-packages (from mpld3->pixiedust) (3.3.3)\n", 30 | "Requirement already satisfied: jinja2 in /opt/conda/lib/python3.7/site-packages (from mpld3->pixiedust) (2.11.0)\n", 31 | "Requirement already satisfied: importlib-metadata; python_version < \"3.8\" in /opt/conda/lib/python3.7/site-packages (from markdown->pixiedust) (1.5.0)\n", 32 | "Requirement already satisfied: urllib3!=1.25.0,!=1.25.1,<1.26,>=1.21.1 in /opt/conda/lib/python3.7/site-packages (from requests->pixiedust) (1.25.7)\n", 33 | "Requirement already satisfied: chardet<3.1.0,>=3.0.2 in /opt/conda/lib/python3.7/site-packages (from requests->pixiedust) (3.0.4)\n", 34 | "Requirement already satisfied: certifi>=2017.4.17 in /opt/conda/lib/python3.7/site-packages (from requests->pixiedust) (2019.11.28)\n", 35 | "Requirement already satisfied: idna<2.9,>=2.5 in /opt/conda/lib/python3.7/site-packages (from requests->pixiedust) (2.8)\n", 36 | "Requirement already satisfied: wheel<1.0,>=0.23.0 in /opt/conda/lib/python3.7/site-packages (from astunparse->pixiedust) (0.34.1)\n", 37 | "Requirement already satisfied: six<2.0,>=1.6.1 in /opt/conda/lib/python3.7/site-packages (from astunparse->pixiedust) (1.14.0)\n", 38 | "Requirement already satisfied: traitlets>=4.2 in /opt/conda/lib/python3.7/site-packages (from ipython->pixiedust_node) (4.3.3)\n", 39 | "Requirement already satisfied: pexpect; sys_platform != \"win32\" in /opt/conda/lib/python3.7/site-packages (from ipython->pixiedust_node) (4.8.0)\n", 40 | "Requirement already satisfied: setuptools>=18.5 in /opt/conda/lib/python3.7/site-packages (from ipython->pixiedust_node) (45.1.0.post20200119)\n", 41 | "Requirement already satisfied: jedi>=0.10 in /opt/conda/lib/python3.7/site-packages (from ipython->pixiedust_node) (0.16.0)\n", 42 | "Requirement already satisfied: prompt-toolkit!=3.0.0,!=3.0.1,<3.1.0,>=2.0.0 in /opt/conda/lib/python3.7/site-packages (from ipython->pixiedust_node) (3.0.3)\n", 43 | "Requirement already satisfied: pygments in /opt/conda/lib/python3.7/site-packages (from ipython->pixiedust_node) (2.5.2)\n", 44 | "Requirement already satisfied: pickleshare in /opt/conda/lib/python3.7/site-packages (from ipython->pixiedust_node) (0.7.5)\n", 45 | "Requirement already satisfied: decorator in /opt/conda/lib/python3.7/site-packages (from ipython->pixiedust_node) (4.4.1)\n", 46 | "Requirement already satisfied: backcall in /opt/conda/lib/python3.7/site-packages (from ipython->pixiedust_node) (0.1.0)\n", 47 | "Requirement already satisfied: pytz>=2017.2 in /opt/conda/lib/python3.7/site-packages (from pandas->pixiedust_node) (2020.4)\n", 48 | "Requirement already satisfied: numpy>=1.15.4 in /opt/conda/lib/python3.7/site-packages (from pandas->pixiedust_node) (1.19.4)\n", 49 | "Requirement already satisfied: python-dateutil>=2.7.3 in /opt/conda/lib/python3.7/site-packages (from pandas->pixiedust_node) (2.8.1)\n", 50 | "Requirement already satisfied: cycler>=0.10 in /opt/conda/lib/python3.7/site-packages (from matplotlib->mpld3->pixiedust) (0.10.0)\n", 51 | "Requirement already satisfied: pyparsing!=2.0.4,!=2.1.2,!=2.1.6,>=2.0.3 in /opt/conda/lib/python3.7/site-packages (from matplotlib->mpld3->pixiedust) (2.4.7)\n", 52 | "Requirement already satisfied: kiwisolver>=1.0.1 in /opt/conda/lib/python3.7/site-packages (from matplotlib->mpld3->pixiedust) (1.3.1)\n", 53 | "Requirement already satisfied: pillow>=6.2.0 in /opt/conda/lib/python3.7/site-packages (from matplotlib->mpld3->pixiedust) (8.0.1)\n", 54 | "Requirement already satisfied: MarkupSafe>=0.23 in /opt/conda/lib/python3.7/site-packages (from jinja2->mpld3->pixiedust) (1.1.1)\n", 55 | "Requirement already satisfied: zipp>=0.5 in /opt/conda/lib/python3.7/site-packages (from importlib-metadata; python_version < \"3.8\"->markdown->pixiedust) (2.1.0)\n", 56 | "Requirement already satisfied: ipython-genutils in /opt/conda/lib/python3.7/site-packages (from traitlets>=4.2->ipython->pixiedust_node) (0.2.0)\n", 57 | "Requirement already satisfied: ptyprocess>=0.5 in /opt/conda/lib/python3.7/site-packages (from pexpect; sys_platform != \"win32\"->ipython->pixiedust_node) (0.6.0)\n", 58 | "Requirement already satisfied: parso>=0.5.2 in /opt/conda/lib/python3.7/site-packages (from jedi>=0.10->ipython->pixiedust_node) (0.6.0)\n", 59 | "Requirement already satisfied: wcwidth in /opt/conda/lib/python3.7/site-packages (from prompt-toolkit!=3.0.0,!=3.0.1,<3.1.0,>=2.0.0->ipython->pixiedust_node) (0.1.8)\n", 60 | "Note: you may need to restart the kernel to use updated packages.\n" 61 | ] 62 | } 63 | ], 64 | "source": [ 65 | "pip install pixiedust pixiedust_node" 66 | ] 67 | }, 68 | { 69 | "cell_type": "markdown", 70 | "metadata": {}, 71 | "source": [ 72 | "# kibibit\n", 73 | "\n", 74 | "## Let's do some node.js inside a document!\n", 75 | "\n", 76 | "First thing first, we need to initialize node by running:" 77 | ] 78 | }, 79 | { 80 | "cell_type": "code", 81 | "execution_count": 2, 82 | "metadata": {}, 83 | "outputs": [ 84 | { 85 | "name": "stdout", 86 | "output_type": "stream", 87 | "text": [ 88 | "Pixiedust database opened successfully\n" 89 | ] 90 | }, 91 | { 92 | "data": { 93 | "text/html": [ 94 | "\n", 95 | "
\n", 96 | " \n", 97 | " \n", 98 | " \n", 99 | " Pixiedust version 1.1.18\n", 100 | "
\n", 101 | " " 102 | ], 103 | "text/plain": [ 104 | "" 105 | ] 106 | }, 107 | "metadata": {}, 108 | "output_type": "display_data" 109 | }, 110 | { 111 | "data": { 112 | "text/html": [ 113 | "\n", 114 | "
\n", 115 | " \n", 116 | " \n", 117 | " \n", 118 | " Pixiedust Node.js \n", 119 | "
\n" 120 | ], 121 | "text/plain": [ 122 | "" 123 | ] 124 | }, 125 | "metadata": {}, 126 | "output_type": "display_data" 127 | } 128 | ], 129 | "source": [ 130 | "import pixiedust_node" 131 | ] 132 | }, 133 | { 134 | "cell_type": "markdown", 135 | "metadata": {}, 136 | "source": [ 137 | "Now, we can run a node script by starting with `%%node` and writing a node script after that" 138 | ] 139 | }, 140 | { 141 | "cell_type": "code", 142 | "execution_count": 10, 143 | "metadata": {}, 144 | "outputs": [ 145 | { 146 | "name": "stdout", 147 | "output_type": "stream", 148 | "text": [ 149 | "\"Hello! I am k1b1b0t\"\n", 150 | "\"2020-11-18T15:55:16.264Z\"\n" 151 | ] 152 | } 153 | ], 154 | "source": [ 155 | "%%node\n", 156 | "var username = 'k1b1b0t';\n", 157 | "print(`Hello! I am ${ username }`);\n", 158 | "print(new Date());" 159 | ] 160 | }, 161 | { 162 | "cell_type": "markdown", 163 | "metadata": {}, 164 | "source": [ 165 | "## Node Modules\n", 166 | "Installing npm modules is as easy as:" 167 | ] 168 | }, 169 | { 170 | "cell_type": "code", 171 | "execution_count": 11, 172 | "metadata": {}, 173 | "outputs": [ 174 | { 175 | "name": "stdout", 176 | "output_type": "stream", 177 | "text": [ 178 | "/opt/conda/bin/npm install -s random-sentence\n", 179 | "+ random-sentence@1.0.4\n", 180 | "updated 1 package and audited 21 packages in 0.745s\n", 181 | "2 packages are looking for funding\n", 182 | "run `npm fund` for details\n", 183 | "found 0 vulnerabilities\n" 184 | ] 185 | } 186 | ], 187 | "source": [ 188 | "npm.install('random-sentence')" 189 | ] 190 | }, 191 | { 192 | "cell_type": "markdown", 193 | "metadata": {}, 194 | "source": [ 195 | "Then using it like so:" 196 | ] 197 | }, 198 | { 199 | "cell_type": "code", 200 | "execution_count": 12, 201 | "metadata": {}, 202 | "outputs": [ 203 | { 204 | "name": "stdout", 205 | "output_type": "stream", 206 | "text": [ 207 | "\"His oti ri cewol kandijuw wijhojok jisapi agfo ba cuvvi decaku kavre no si sut ojmik zifro.\"\n" 208 | ] 209 | } 210 | ], 211 | "source": [ 212 | "%%node\n", 213 | "var randomSentence = require('random-sentence');\n", 214 | "print(randomSentence());" 215 | ] 216 | }, 217 | { 218 | "cell_type": "markdown", 219 | "metadata": {}, 220 | "source": [ 221 | "## Available Functions for output\n", 222 | "Node.js functions are available to interact with the Notebook\n", 223 | "\n", 224 | "- print(x) - print out the value of variable x\n", 225 | "- display(x) - use Pixiedust's display function to visualise an array of data\n", 226 | "- store(x,'y') - turn a JavaScript array x into a Pandas data frame and store in Python variable y\n", 227 | "- html(x) - render HTML string x in a notebook cell\n", 228 | "- image(x) - render image URL x in a notebook cell\n", 229 | "- help() - show help" 230 | ] 231 | }, 232 | { 233 | "cell_type": "markdown", 234 | "metadata": {}, 235 | "source": [ 236 | "## Some Examples (Drawing stuff and graphs)\n", 237 | "Because you can import whatever you want, you can draw graphs with libraries that generates them like `chart.js`, `d3.js`, and `quiche`:" 238 | ] 239 | }, 240 | { 241 | "cell_type": "code", 242 | "execution_count": 13, 243 | "metadata": {}, 244 | "outputs": [ 245 | { 246 | "name": "stdout", 247 | "output_type": "stream", 248 | "text": [ 249 | "/opt/conda/bin/npm install -s quiche\n", 250 | "+ quiche@0.3.0\n", 251 | "updated 1 package and audited 21 packages in 1.387s\n", 252 | "2 packages are looking for funding\n", 253 | "run `npm fund` for details\n", 254 | "found 0 vulnerabilities\n" 255 | ] 256 | } 257 | ], 258 | "source": [ 259 | "npm.install('quiche');" 260 | ] 261 | }, 262 | { 263 | "cell_type": "code", 264 | "execution_count": 14, 265 | "metadata": {}, 266 | "outputs": [ 267 | { 268 | "data": { 269 | "text/html": [ 270 | "" 271 | ], 272 | "text/plain": [ 273 | "" 274 | ] 275 | }, 276 | "metadata": {}, 277 | "output_type": "display_data" 278 | } 279 | ], 280 | "source": [ 281 | "%%node\n", 282 | "var quiche = require('quiche');\n", 283 | "var qr = quiche('qr');\n", 284 | "qr.setLabel('https://github.com/ibm-cds-labs/pixiedust_node');\n", 285 | "image(qr.getUrl());" 286 | ] 287 | }, 288 | { 289 | "cell_type": "code", 290 | "execution_count": 15, 291 | "metadata": {}, 292 | "outputs": [ 293 | { 294 | "data": { 295 | "text/html": [ 296 | "" 297 | ], 298 | "text/plain": [ 299 | "" 300 | ] 301 | }, 302 | "metadata": {}, 303 | "output_type": "display_data" 304 | } 305 | ], 306 | "source": [ 307 | "%%node\n", 308 | "var Quiche = require('quiche');\n", 309 | "\n", 310 | " var bar = new Quiche('bar');\n", 311 | " bar.setWidth(400);\n", 312 | " bar.setHeight(265);\n", 313 | " bar.setTitle('Some title or something');\n", 314 | " bar.setBarStacked(); // Stacked chart\n", 315 | " bar.setBarWidth(0);\n", 316 | " bar.setBarSpacing(6); // 6 pixles between bars/groups\n", 317 | " bar.setLegendBottom(); // Put legend at bottom\n", 318 | " bar.setTransparentBackground(); // Make background transparent\n", 319 | "\n", 320 | " bar.addData([19, 19, 21, 14, 19, 11, 10, 18, 19, 30], 'Foo', 'FF3860');\n", 321 | " bar.addData([4, 3, 2, 3, 0, 0, 3, 4, 2, 2], 'bar', '209CEE');\n", 322 | " bar.addData([10, 8, 2, 1, 18, 9, 20, 21, 19, 11], 'bin', '00D1B2');\n", 323 | " bar.addData([2, 1, 1, 1, 1, 7, 3, 6, 2, 7], 'bash', '48C774');\n", 324 | " bar.addData([1, 0, 0, 1, 2, 1, 0, 0, 0, 0], 'blah', 'FFDD57'); \n", 325 | "\n", 326 | " bar.setAutoScaling(); // Auto scale y axis\n", 327 | " bar.addAxisLabels('x', ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10']);\n", 328 | "\n", 329 | "image(bar.getUrl());" 330 | ] 331 | }, 332 | { 333 | "cell_type": "code", 334 | "execution_count": null, 335 | "metadata": {}, 336 | "outputs": [], 337 | "source": [] 338 | } 339 | ], 340 | "metadata": { 341 | "kernelspec": { 342 | "display_name": "Python 3", 343 | "language": "python", 344 | "name": "python3" 345 | }, 346 | "language_info": { 347 | "codemirror_mode": { 348 | "name": "ipython", 349 | "version": 3 350 | }, 351 | "file_extension": ".py", 352 | "mimetype": "text/x-python", 353 | "name": "python", 354 | "nbconvert_exporter": "python", 355 | "pygments_lexer": "ipython3", 356 | "version": "3.7.4" 357 | } 358 | }, 359 | "nbformat": 4, 360 | "nbformat_minor": 4 361 | } 362 | --------------------------------------------------------------------------------