A series of experiments with Julia and Python
0

Configure Feed

Select the types of activity you want to include in your feed.

julia-playground / packages / Interact.ipynb
12 kB 580 lines
1{ 2 "cells": [ 3 { 4 "cell_type": "code", 5 "execution_count": 8, 6 "metadata": { 7 "collapsed": false 8 }, 9 "outputs": [ 10 { 11 "name": "stdout", 12 "output_type": "stream", 13 "text": [ 14 "Requirement already satisfied (use --upgrade to upgrade): ipywidgets in /Users/utensil/miniconda2/lib/python2.7/site-packages\n" 15 ] 16 }, 17 { 18 "name": "stderr", 19 "output_type": "stream", 20 "text": [ 21 "You are using pip version 8.1.1, however version 8.1.2 is available.\n", 22 "You should consider upgrading via the 'pip install --upgrade pip' command.\n" 23 ] 24 } 25 ], 26 "source": [ 27 "run(`pip install ipywidgets`)" 28 ] 29 }, 30 { 31 "cell_type": "code", 32 "execution_count": 16, 33 "metadata": { 34 "collapsed": false 35 }, 36 "outputs": [ 37 { 38 "name": "stderr", 39 "output_type": "stream", 40 "text": [ 41 "INFO: Nothing to be done\n", 42 "INFO: Nothing to be done\n", 43 "INFO: No packages to install, update or remove\n", 44 "INFO: Package database updated\n" 45 ] 46 } 47 ], 48 "source": [ 49 "Pkg.add(\"Interact\")\n", 50 "Pkg.add(\"Gadfly\")\n", 51 "Pkg.add(\"Colors\")" 52 ] 53 }, 54 { 55 "cell_type": "code", 56 "execution_count": 17, 57 "metadata": { 58 "collapsed": false 59 }, 60 "outputs": [], 61 "source": [ 62 "using Reactive, Interact, Colors" 63 ] 64 }, 65 { 66 "cell_type": "code", 67 "execution_count": 9, 68 "metadata": { 69 "collapsed": false 70 }, 71 "outputs": [ 72 { 73 "data": { 74 "text/html": [], 75 "text/plain": [ 76 "Interact.Slider{Float64}(Signal{Float64}(0.5, nactions=0),\"Slider X:\",0.5,0.0:0.1:1.0,true)" 77 ] 78 }, 79 "execution_count": 9, 80 "metadata": {}, 81 "output_type": "execute_result" 82 } 83 ], 84 "source": [ 85 "s = slider(0:.1:1,label=\"Slider X:\")" 86 ] 87 }, 88 { 89 "cell_type": "code", 90 "execution_count": 10, 91 "metadata": { 92 "collapsed": false 93 }, 94 "outputs": [ 95 { 96 "data": { 97 "text/plain": [ 98 "0.5" 99 ] 100 }, 101 "execution_count": 10, 102 "metadata": { 103 "comm_id": "c2860b98-5944-40b7-8343-64a4ecb95d05", 104 "reactive": true 105 }, 106 "output_type": "execute_result" 107 } 108 ], 109 "source": [ 110 "signal(s)" 111 ] 112 }, 113 { 114 "cell_type": "code", 115 "execution_count": 11, 116 "metadata": { 117 "collapsed": false 118 }, 119 "outputs": [ 120 { 121 "data": { 122 "text/plain": [ 123 "Interact.Slider{Float64}" 124 ] 125 }, 126 "metadata": {}, 127 "output_type": "display_data" 128 }, 129 { 130 "data": { 131 "text/plain": [ 132 "true" 133 ] 134 }, 135 "execution_count": 11, 136 "metadata": {}, 137 "output_type": "execute_result" 138 } 139 ], 140 "source": [ 141 "display(typeof(s));\n", 142 "isa(s, Widget)" 143 ] 144 }, 145 { 146 "cell_type": "code", 147 "execution_count": 12, 148 "metadata": { 149 "collapsed": false 150 }, 151 "outputs": [ 152 { 153 "data": { 154 "text/plain": [ 155 "Reactive.Signal{Float64}" 156 ] 157 }, 158 "metadata": {}, 159 "output_type": "display_data" 160 }, 161 { 162 "data": { 163 "text/plain": [ 164 "true" 165 ] 166 }, 167 "execution_count": 12, 168 "metadata": {}, 169 "output_type": "execute_result" 170 } 171 ], 172 "source": [ 173 "display(typeof(signal(s)));\n", 174 "isa(signal(s), Signal{Float64})" 175 ] 176 }, 177 { 178 "cell_type": "code", 179 "execution_count": 13, 180 "metadata": { 181 "collapsed": false 182 }, 183 "outputs": [ 184 { 185 "data": { 186 "text/html": [], 187 "text/plain": [ 188 "Interact.Slider{Float64}(Signal{Float64}(0.6, nactions=1),\"Slider X:\",0.6,0.0:0.1:1.0,true)" 189 ] 190 }, 191 "execution_count": 13, 192 "metadata": {}, 193 "output_type": "execute_result" 194 } 195 ], 196 "source": [ 197 "s" 198 ] 199 }, 200 { 201 "cell_type": "code", 202 "execution_count": 15, 203 "metadata": { 204 "collapsed": false 205 }, 206 "outputs": [ 207 { 208 "data": { 209 "text/plain": [ 210 "0.16000000000000003" 211 ] 212 }, 213 "execution_count": 15, 214 "metadata": { 215 "comm_id": "3b8f53d9-50cd-4f97-9683-19cb7b30f97c", 216 "reactive": true 217 }, 218 "output_type": "execute_result" 219 } 220 ], 221 "source": [ 222 "xsquared = map(x -> x*x, signal(s))" 223 ] 224 }, 225 { 226 "cell_type": "code", 227 "execution_count": 23, 228 "metadata": { 229 "collapsed": false 230 }, 231 "outputs": [ 232 { 233 "data": { 234 "image/svg+xml": [ 235 "<?xml version\"1.0\" encoding=\"UTF-8\"?>\n", 236 "<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"\n", 237 " \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n", 238 "<svg xmlns=\"http://www.w3.org/2000/svg\" version=\"1.1\"\n", 239 " width=\"25mm\" height=\"25mm\" viewBox=\"0 0 1 1\">\n", 240 " <rect width=\"1\" height=\"1\"\n", 241 " fill=\"#998080\" stroke=\"none\"/>\n", 242 "</svg>\n" 243 ], 244 "text/plain": [ 245 "RGB{Float64}(0.6,0.5,0.5)" 246 ] 247 }, 248 "execution_count": 23, 249 "metadata": { 250 "comm_id": "382eb97a-d0d8-4ca2-a81f-2c9210b9b81c", 251 "reactive": true 252 }, 253 "output_type": "execute_result" 254 } 255 ], 256 "source": [ 257 "map(x -> RGB(x, 0.5, 0.5), signal(s))" 258 ] 259 }, 260 { 261 "cell_type": "code", 262 "execution_count": 24, 263 "metadata": { 264 "collapsed": false 265 }, 266 "outputs": [ 267 { 268 "data": { 269 "text/html": [], 270 "text/plain": [ 271 "Interact.Slider{Float64}(Signal{Float64}(0.5, nactions=0),\"R\",0.5,0.0:0.01:1.0,true)" 272 ] 273 }, 274 "metadata": {}, 275 "output_type": "display_data" 276 }, 277 { 278 "data": { 279 "text/html": [], 280 "text/plain": [ 281 "Interact.Slider{Float64}(Signal{Float64}(0.5, nactions=0),\"G\",0.5,0.0:0.01:1.0,true)" 282 ] 283 }, 284 "metadata": {}, 285 "output_type": "display_data" 286 }, 287 { 288 "data": { 289 "text/html": [], 290 "text/plain": [ 291 "Interact.Slider{Float64}(Signal{Float64}(0.5, nactions=0),\"B\",0.5,0.0:0.01:1.0,true)" 292 ] 293 }, 294 "metadata": {}, 295 "output_type": "display_data" 296 } 297 ], 298 "source": [ 299 "r = slider(0:0.01:1, label=\"R\")\n", 300 "g = slider(0:0.01:1, label=\"G\")\n", 301 "b = slider(0:0.01:1, label=\"B\")\n", 302 "map(display, [r,g,b]);" 303 ] 304 }, 305 { 306 "cell_type": "code", 307 "execution_count": 25, 308 "metadata": { 309 "collapsed": false 310 }, 311 "outputs": [ 312 { 313 "data": { 314 "image/svg+xml": [ 315 "<?xml version\"1.0\" encoding=\"UTF-8\"?>\n", 316 "<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"\n", 317 " \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n", 318 "<svg xmlns=\"http://www.w3.org/2000/svg\" version=\"1.1\"\n", 319 " width=\"25mm\" height=\"25mm\" viewBox=\"0 0 1 1\">\n", 320 " <rect width=\"1\" height=\"1\"\n", 321 " fill=\"#808080\" stroke=\"none\"/>\n", 322 "</svg>\n" 323 ], 324 "text/plain": [ 325 "RGB{Float64}(0.5,0.5,0.5)" 326 ] 327 }, 328 "execution_count": 25, 329 "metadata": { 330 "comm_id": "d352cc8d-9160-4b32-83bd-41c2a967efde", 331 "reactive": true 332 }, 333 "output_type": "execute_result" 334 } 335 ], 336 "source": [ 337 "color = map((x, y, z) -> RGB(x, y, z), signal(r), signal(g), signal(b))" 338 ] 339 }, 340 { 341 "cell_type": "code", 342 "execution_count": 27, 343 "metadata": { 344 "collapsed": false 345 }, 346 "outputs": [ 347 { 348 "data": { 349 "text/html": [ 350 "<div style='color:#808080'>Hello, World!</div>" 351 ], 352 "text/plain": [ 353 "HTML{ASCIIString}(\"<div style='color:#808080'>Hello, World!</div>\")" 354 ] 355 }, 356 "execution_count": 27, 357 "metadata": { 358 "comm_id": "c52ecfbe-2b08-4981-bc0e-cffb6c8e5846", 359 "reactive": true 360 }, 361 "output_type": "execute_result" 362 } 363 ], 364 "source": [ 365 "map(color -> HTML(string(\"<div style='color:#\", hex(color), \"'>Hello, World!</div>\")), signal(color))" 366 ] 367 }, 368 { 369 "cell_type": "code", 370 "execution_count": 28, 371 "metadata": { 372 "collapsed": false 373 }, 374 "outputs": [ 375 { 376 "data": { 377 "text/html": [], 378 "text/plain": [ 379 "Interact.Slider{Float64}(Signal{Float64}(0.5, nactions=0),\"r\",0.5,0.0:0.05:1.0,true)" 380 ] 381 }, 382 "metadata": {}, 383 "output_type": "display_data" 384 }, 385 { 386 "data": { 387 "text/html": [], 388 "text/plain": [ 389 "Interact.Slider{Float64}(Signal{Float64}(0.5, nactions=0),\"g\",0.5,0.0:0.05:1.0,true)" 390 ] 391 }, 392 "metadata": {}, 393 "output_type": "display_data" 394 }, 395 { 396 "data": { 397 "text/html": [], 398 "text/plain": [ 399 "Interact.Slider{Float64}(Signal{Float64}(0.5, nactions=0),\"b\",0.5,0.0:0.05:1.0,true)" 400 ] 401 }, 402 "metadata": {}, 403 "output_type": "display_data" 404 }, 405 { 406 "data": { 407 "text/html": [ 408 "<div style='color:#808080'>Color me</div>" 409 ], 410 "text/plain": [ 411 "HTML{ASCIIString}(\"<div style='color:#808080'>Color me</div>\")" 412 ] 413 }, 414 "execution_count": 28, 415 "metadata": { 416 "comm_id": "a38702bb-6618-439c-a023-811b4ac01dbb", 417 "reactive": true 418 }, 419 "output_type": "execute_result" 420 } 421 ], 422 "source": [ 423 "@manipulate for r = 0:.05:1, g = 0:.05:1, b = 0:.05:1\n", 424 " HTML(string(\"<div style='color:#\", hex(RGB(r,g,b)), \"'>Color me</div>\"))\n", 425 "end" 426 ] 427 }, 428 { 429 "cell_type": "code", 430 "execution_count": 29, 431 "metadata": { 432 "collapsed": false 433 }, 434 "outputs": [ 435 { 436 "data": { 437 "text/html": [], 438 "text/plain": [ 439 "Interact.Slider{Float64}(Signal{Float64}(3.1, nactions=2),\"x\",3.1,0.0:0.1:6.2,true)" 440 ] 441 }, 442 "metadata": {}, 443 "output_type": "display_data" 444 }, 445 { 446 "data": { 447 "text/html": [], 448 "text/plain": [ 449 "Interact.Slider{Float64}(Signal{Float64}(-0.0830894028174964, nactions=0),\"sin(2x)\",-0.0830894028174964,-1.0:0.05:1.0,true)" 450 ] 451 }, 452 "metadata": {}, 453 "output_type": "display_data" 454 }, 455 { 456 "data": { 457 "text/html": [], 458 "text/plain": [ 459 "Interact.Slider{Float64}(Signal{Float64}(0.9965420970232175, nactions=0),\"cos(2x)\",0.9965420970232175,-1.0:0.05:1.0,true)" 460 ] 461 }, 462 "metadata": {}, 463 "output_type": "display_data" 464 } 465 ], 466 "source": [ 467 "x = slider(0:.1:2pi, label=\"x\")\n", 468 "s = map(a -> slider(-1:.05:1, value=sin(2a), label=\"sin(2x)\"), signal(x))\n", 469 "c = map(a -> slider(-1:.05:1, value=cos(2a), label=\"cos(2x)\"), signal(x))\n", 470 "map(display, [x,s,c]);" 471 ] 472 }, 473 { 474 "cell_type": "code", 475 "execution_count": 30, 476 "metadata": { 477 "collapsed": false 478 }, 479 "outputs": [ 480 { 481 "data": { 482 "text/plain": [ 483 "0.0" 484 ] 485 }, 486 "execution_count": 30, 487 "metadata": { 488 "comm_id": "28be9a05-5ff4-44e6-88d7-96ae75a78c22", 489 "reactive": true 490 }, 491 "output_type": "execute_result" 492 } 493 ], 494 "source": [ 495 "fx = Signal(0.0) # A float input" 496 ] 497 }, 498 { 499 "cell_type": "code", 500 "execution_count": 38, 501 "metadata": { 502 "collapsed": false 503 }, 504 "outputs": [ 505 { 506 "data": { 507 "text/html": [], 508 "text/plain": [ 509 "Interact.Slider{Float64}(Signal{Float64}(3.1, nactions=1),\"x\",3.1,0.0:0.1:6.2,true)" 510 ] 511 }, 512 "metadata": {}, 513 "output_type": "display_data" 514 }, 515 { 516 "data": { 517 "text/html": [], 518 "text/plain": [ 519 "Interact.Slider{Float64}(Signal{Float64}(0.0, nactions=1),\"f(x)\",0.04158066243329049,-1.0:0.05:1.0,true)" 520 ] 521 }, 522 "metadata": {}, 523 "output_type": "display_data" 524 } 525 ], 526 "source": [ 527 "x = slider(0:.1:2pi, label=\"x\")\n", 528 "y = map(v -> slider(-1:.05:1, value=sin(v), signal=fx, label=\"f(x)\"), signal(x))\n", 529 "map(display, (x,y));" 530 ] 531 }, 532 { 533 "cell_type": "code", 534 "execution_count": 37, 535 "metadata": { 536 "collapsed": false 537 }, 538 "outputs": [ 539 { 540 "data": { 541 "text/html": [ 542 "<img src=\"http://i.imgur.com/PzOz1qc.gif\" />\n" 543 ] 544 }, 545 "metadata": {}, 546 "output_type": "display_data" 547 } 548 ], 549 "source": [ 550 "display(IJulia.text_html, \"\"\"\n", 551 "<img src=\"http://i.imgur.com/PzOz1qc.gif\" />\n", 552 "\"\"\")" 553 ] 554 }, 555 { 556 "cell_type": "code", 557 "execution_count": null, 558 "metadata": { 559 "collapsed": true 560 }, 561 "outputs": [], 562 "source": [] 563 } 564 ], 565 "metadata": { 566 "kernelspec": { 567 "display_name": "Julia 0.4.5", 568 "language": "julia", 569 "name": "julia-0.4" 570 }, 571 "language_info": { 572 "file_extension": ".jl", 573 "mimetype": "application/julia", 574 "name": "julia", 575 "version": "0.4.5" 576 } 577 }, 578 "nbformat": 4, 579 "nbformat_minor": 0 580}