|
18 | 18 | "cell_type": "code",
|
19 | 19 | "execution_count": null,
|
20 | 20 | "metadata": {
|
21 |
| - "tags": ["remove-cell"] |
| 21 | + "tags": [ |
| 22 | + "remove-cell" |
| 23 | + ] |
22 | 24 | },
|
23 | 25 | "outputs": [],
|
24 | 26 | "source": [
|
|
353 | 355 | "interact(f, x=widgets.Combobox(options=[\"Chicago\", \"New York\", \"Washington\"], value=\"Chicago\"));"
|
354 | 356 | ]
|
355 | 357 | },
|
| 358 | + { |
| 359 | + "cell_type": "markdown", |
| 360 | + "metadata": {}, |
| 361 | + "source": [ |
| 362 | + "## Type Annotations" |
| 363 | + ] |
| 364 | + }, |
| 365 | + { |
| 366 | + "cell_type": "markdown", |
| 367 | + "metadata": {}, |
| 368 | + "source": [ |
| 369 | + "If the function that you are using with interact uses type annotations, `interact` may be able to use those to determine what UI components to use in the auto-generated UI. For example, given a function with an argument annotated with type `float`" |
| 370 | + ] |
| 371 | + }, |
| 372 | + { |
| 373 | + "cell_type": "code", |
| 374 | + "execution_count": null, |
| 375 | + "metadata": {}, |
| 376 | + "outputs": [], |
| 377 | + "source": [ |
| 378 | + "def f(x: float):\n", |
| 379 | + " return x" |
| 380 | + ] |
| 381 | + }, |
| 382 | + { |
| 383 | + "cell_type": "markdown", |
| 384 | + "metadata": {}, |
| 385 | + "source": [ |
| 386 | + "then `interact` will create a UI with a `FloatText` component without needing to be passed any values or abbreviations." |
| 387 | + ] |
| 388 | + }, |
| 389 | + { |
| 390 | + "cell_type": "code", |
| 391 | + "execution_count": null, |
| 392 | + "metadata": {}, |
| 393 | + "outputs": [], |
| 394 | + "source": [ |
| 395 | + "interact(f);" |
| 396 | + ] |
| 397 | + }, |
| 398 | + { |
| 399 | + "cell_type": "markdown", |
| 400 | + "metadata": {}, |
| 401 | + "source": [ |
| 402 | + "The following table gives an overview of different annotation types, and how they map to interactive controls:\n", |
| 403 | + "\n", |
| 404 | + "<table class=\"table table-condensed table-bordered\">\n", |
| 405 | + " <tr><td><strong>Type Annotation</strong></td><td><strong>Widget</strong></td></tr> \n", |
| 406 | + " <tr><td>`bool`</td><td>Checkbox</td></tr> \n", |
| 407 | + " <tr><td>`str`</td><td>Text</td></tr>\n", |
| 408 | + " <tr><td>`int`</td><td>IntText</td></tr>\n", |
| 409 | + " <tr><td>`float`</td><td>FloatText</td></tr>\n", |
| 410 | + " <tr><td>`Enum` subclasses</td><td>Dropdown</td></tr>\n", |
| 411 | + "</table>\n", |
| 412 | + "\n", |
| 413 | + "Other type annotations are ignored." |
| 414 | + ] |
| 415 | + }, |
| 416 | + { |
| 417 | + "cell_type": "markdown", |
| 418 | + "metadata": {}, |
| 419 | + "source": [ |
| 420 | + "If values or abbreviations are passed to the `interact` function, those will override any type annotations when determining what widgets to create." |
| 421 | + ] |
| 422 | + }, |
| 423 | + { |
| 424 | + "cell_type": "markdown", |
| 425 | + "metadata": {}, |
| 426 | + "source": [ |
| 427 | + "Parameters which are annotationed with an `Enum` subclass will have a dropdown created whose labels are the names of the enumeration and which pass the corresponding values to the function parameter." |
| 428 | + ] |
| 429 | + }, |
| 430 | + { |
| 431 | + "cell_type": "code", |
| 432 | + "execution_count": null, |
| 433 | + "metadata": {}, |
| 434 | + "outputs": [], |
| 435 | + "source": [ |
| 436 | + "from enum import Enum\n", |
| 437 | + "\n", |
| 438 | + "class Color(Enum):\n", |
| 439 | + " red = 0\n", |
| 440 | + " green = 1\n", |
| 441 | + " blue = 2\n", |
| 442 | + "\n", |
| 443 | + "def h(color: Color):\n", |
| 444 | + " return color" |
| 445 | + ] |
| 446 | + }, |
| 447 | + { |
| 448 | + "cell_type": "markdown", |
| 449 | + "metadata": {}, |
| 450 | + "source": [ |
| 451 | + "When `interact` is used with the function `h`, the Dropdown widget it creates will have options `\"red\"`, `\"green\"` and `\"blue\"` and the values passed to the function will be, correspondingly, `Color.red`, `Color.green` and `Color.blue`." |
| 452 | + ] |
| 453 | + }, |
| 454 | + { |
| 455 | + "cell_type": "code", |
| 456 | + "execution_count": null, |
| 457 | + "metadata": {}, |
| 458 | + "outputs": [], |
| 459 | + "source": [ |
| 460 | + "interact(h);" |
| 461 | + ] |
| 462 | + }, |
356 | 463 | {
|
357 | 464 | "cell_type": "markdown",
|
358 | 465 | "metadata": {},
|
|
715 | 822 | },
|
716 | 823 | {
|
717 | 824 | "cell_type": "code",
|
718 |
| - "execution_count": 1, |
| 825 | + "execution_count": null, |
719 | 826 | "metadata": {},
|
720 | 827 | "outputs": [],
|
721 | 828 | "source": [
|
|
762 | 869 | "name": "python",
|
763 | 870 | "nbconvert_exporter": "python",
|
764 | 871 | "pygments_lexer": "ipython3",
|
765 |
| - "version": "3.10.5" |
| 872 | + "version": "3.12.2" |
766 | 873 | }
|
767 | 874 | },
|
768 | 875 | "nbformat": 4,
|
769 |
| - "nbformat_minor": 2 |
| 876 | + "nbformat_minor": 4 |
770 | 877 | }
|
0 commit comments