/usr/lib/python2.7/dist-packages/sagenb/notebook/interact.py is in python-sagenb 1.0.1+ds1-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 | # -*- coding: utf-8 -*
#############################################################################
# Copyright (C) 2008 William Stein <wstein@gmail.com>
# Distributed under the terms of the GNU General Public License (GPL)
# The full text of the GPL is available at:
# http://www.gnu.org/licenses/
#############################################################################
r"""
Interact Functions in the Notebook
This module implements an :func:`interact` function decorator for the Sage
notebook.
AUTHORS:
- William Stein (2008-03-02): version 1.0 at Sage/Enthought Days
8 in Texas
- Jason Grout (2008-03): discussion and first few prototypes
- Jason Grout (2008-05): :class:`input_grid` control
"""
"""
** PLANNING **
NOTES:
* There is no testing of pickling anywhere in this file. This is
because there is no reason one would ever pickle anything in this
file, since everything is associated with particular state
information of a notebook.
BUGS:
[x] have default values set from the get go
[x] spacing around sliders; also need to have labels
[x] when re-evaluate input, make sure to clear output so cell-interact-id div is gone.
[x] two interacts in one cell -- what to do?
[x] draw initial state
[x] make interact canvas resizable
[x] if you use a interact control after restarting, doesn't work. Need to reset it. How?
[x] display html parts of output as html
[x] default slider pos doesn't work, eg. def _(q1=(-1,(-3,3)), q2=(1,(-3,3))):
[x] change from interact to interact everywhere.
[x] edit/save breaks interact mode
* picking up images that shouldn't.
* controls completely stop working.
[x] problems with html/pre/text formatting, e.g., in TEXT mode and in interact cells
[x] tab completion in interact broken formatting
[x] error exception reporting broken
[x] replace special %interact by something very obfuscated to keep from having
really weird mistakes that are hard for people to debug.
[x] cell order corruption
[x] cross-platform testing (good enough -- it's jquery)
[x] can't enter "foo" in input_box now because of how strings are
passed back and forth using single quotes.
[x] possible issue with page title being undefined; don't know why
or if that is connected with interactives
[x] autorunning interact cells on load is being injected into the
i/o pexpect stream way too early.
[x] what do published worksheets do??
VERSION 1:
[X] get sliders to work; values after move slider
[x] default values
[x] NO -- autoswitch to 1-cell mode:
put slide_mode(); jump_to_slide(%s); in wrap_in_outside_frame
but feels all wrong.
[x] completely get rid of left clicking to switch wrap mode for
interact objects: always in word wrap mode or hide.
[x] shortcut ('label', v)
[x] test saving and loading whole notebook to a file
[x] collection of about 20 good examples of how to use interact (doctested)
[x] interact(f) should also work; i.e., no need to use decorators -- done; won't be advertised, but
at least fixing this improves code quality.
[x] obfuscate ?START and ?END much more.
[x] type checked input box
[x] line up all the control in a single table so all labels and all
controls exactly match up
[x] button bar
[x] drop down menu
[x] checkbox
[x] color selector
[x] something to avoid server flood, e.g., any %interact request removes all other
such requests from the queue in worksheet.py
DOCS:
[x] 100% documentation and doctest coverage
[ ] put the docs for this in the reference manual
[ ] put summary doc in notebook help page
VERSION 2:
[ ] vertical scroll bars (maybe very easy via jsquery)
[ ] small version of color selector
[ ] button -- block of code gets run when it is clicked
[ ] when click a button in a button bar it is highlighted and
other buttons are not (via some sort of javascript)
[ ] much more fine control over style of all controls
[ ] safe/secure evaluation mode
[ ] slider is too narrow -- need to expand to window width?
[ ] fix the flicker resize during update (hard???)
[ ] make option for text input that correctly gives something of
the same type as the default input.
[ ] matrix input control (method of matrix space) -- a spreadsheet like thing
[ ] a 2-D slider:
u = ((xmin,xmax),(ymin,ymax)) 2-D slider -- NOT IMPLEMENTED
[ ] locator in a 2-D graphic
[ ] tab_view -- represents an object in which clicking the tab
with label lbl[i] displays expr[i]
[ ] controls: make them easy to customize as below --
location -- where to put the slider (?)
align -- left, right, top, texttop, middle, absmiddle, baseline, bottom, absbottom
background -- the color of the background for the cell
frame -- draw a frame around
disabled -- disables the input element when it first loads
so that the user can not write text in it, or
select it.
editable -- bool
font_size -- integer
maxlength -- the maximum number of characters allowed in a text field.
name -- defines a unique name for the input element
size -- the size of the input element
type -- button, checkbox, file, password, radio, slider, text, setter_bar
VERSION 3:
[ ] protocol for objects to have their own interact function; make
it so for any object obj in sage, one can do
{{{
interact(obj)
}}}
and get something useful as a result.
[ ] flot -- some pretty but very simple javascript only graphics (maybe don't do... unclear)
[ ] zorn -- similar (maybe don't do... unclear)
[ ] color sliders (?):
u = color(...) a color slider
http://interface.eyecon.ro/demos/slider_colorpicker.html
http://jqueryfordesigners.com/demo/slider-gallery.html
[ ] tag_cell('foo') -- makes it so one can refer to the current cell
from elsewhere using the tag foo instead of the cell id number
This involves doing something with SAGE_CELL_ID and the cell_id() method.
[ ] framed -- put a frame around an object
"""
# Standard system libraries
import inspect
import math
import types
import collections
from base64 import standard_b64decode
# Sage libraries
from sagenb.misc.misc import sage_eval, Color, is_Matrix
from sage.arith.srange import srange
from sage.misc.cachefunc import cached_method
# SAGE_CELL_ID is a module scope variable that is always set equal to
# the current cell id (of the executing cell). Code that sets this is
# inserted by the notebook server each time a worksheet cell is
# evaluated.
SAGE_CELL_ID = 0
# Prefixed to cell input, signals an interact update.
INTERACT_UPDATE_PREFIX = '%__sage_interact__'
# If this special message appears in an interact cell's output, it
# should trigger an automatic re-evaluation of the ambient cell.
INTERACT_RESTART = '__SAGE_INTERACT_RESTART__'
# Place-holder markers/fields, replaced in cell.py and
# notebook_lib.js.
INTERACT_START = '<?__SAGE__START>'
INTERACT_TEXT = '<?__SAGE__TEXT>'
INTERACT_HTML = '<?__SAGE__HTML>'
INTERACT_END = '<?__SAGE__END>'
# Dictionary that stores the state of all active interact cells.
state = {}
def reset_state():
"""
Reset the :func:`interact` state of this sage process.
EXAMPLES::
sage: sagenb.notebook.interact.state # random output
{1: {'function': <function g at 0x72aaab0>, 'variables': {'m': 3, 'n': 5}, 'adapt': {1: <bound method Slider._adaptor of Slider Interact Control: n [1--|1|---10].>, 2: <bound method Slider._adaptor of Slider Interact Control: m [1--|1|---10].>}}}
sage: from sagenb.notebook.interact import reset_state
sage: reset_state()
sage: sagenb.notebook.interact.state
{}
"""
global state
state = {}
_k = 0
def new_adapt_number():
"""
Return an integer, always counting up, and starting with 0. This
is used for saving the adapt methods for controls. An adapt
method is just a function that coerces data into some object,
e.g., makes sure the control always produces int's.
OUTPUT:
- an integer
EXAMPLES::
sage: sagenb.notebook.interact.new_adapt_number() # random output -- depends on when called
1
"""
global _k
_k += 1
return _k
def html(s):
"""
Print the input string ``s`` in a form that tells the notebook to
display it in the HTML portion of the output. This function has
no return value.
INPUT:
- ``s`` - a string
EXAMPLES::
sage: sagenb.notebook.interact.html('hello')
<html>hello</html>
"""
print("<html>%s</html>" % s)
def html_slider(id, values, callback, steps, default=0, margin=0):
"""
Return the HTML representation of a jQuery slider.
INPUT:
- ``id`` - a string; the DOM ID of the slider (better be unique)
- ``values`` - a string; 'null' or JavaScript string containing
array of values on slider
- ``callback`` - a string; JavaScript that is executed whenever
the slider is done moving
- ``steps`` - an integer; number of steps from minimum to maximum
value
- ``default`` - an integer (default: 0); the default position of
the slider
- ``margin`` - an integer (default: 0); size of margin to insert
around the slider
OUTPUT:
- a string - HTML format
EXAMPLES:
We create a jQuery HTML slider. If you do the following in the
notebook you should obtain a slider that when moved pops up a
window showing its current position::
sage: from sagenb.notebook.interact import html_slider, html
sage: html(html_slider('slider-007', 'null', 'alert(position)', steps=5, default=2, margin=5))
<html>...slider...</html>
"""
val_html = ''
if values != 'null':
val_html = """
<td>
<font color="black" id="%s-lbl"></font>
</td>""" % id
s = """
<table>
<tr>
<td>
<div id="%s" style="margin:%spx; margin-left: 1.0em; margin-right: 1.0em; width: 15.0em;"></div>
</td>
%s
</tr>
</table>""" % (id, int(margin), val_html)
# We now generate javascript that gets run after the above div
# gets inserted. This happens because of the setTimeout function
# below which gets passed an anonymous function.
s += """<script>
(function() {
var values = %(values)s;
setTimeout(function() {
$('#%(id)s').slider({
step: 1,
min: 0,
max: %(maxvalue)s,
value: %(startvalue)s,
change: function (e, ui) {
var position = ui.value;
if (values != null) {
$('#%(id)s-lbl').text(values[position]);
%(callback)s;
}
},
slide: function (e, ui) {
if (values != null) {
$('#%(id)s-lbl').text(values[ui.value]);
}
}
});
if (values != null) {
$('#%(id)s-lbl').text(values[$('#%(id)s').slider('value')]);
}
}, 1);
})();
</script>""" % {'values': values, 'id': id, 'maxvalue': steps - 1,
'startvalue': default, 'callback': callback}
# change 'change' to 'slide' and it changes the slider every time it moves;
# needs much more work to actually work, since server gets flooded by
# requests.
return s
def html_rangeslider(id, values, callback, steps, default_l=0, default_r=1,
margin=0):
"""
Return the HTML representation of a jQuery range slider.
INPUT:
- ``id`` - a string; the DOM ID of the slider (better be unique)
- ``values`` - a string; 'null' or JavaScript string containing
array of values on slider
- ``callback`` - a string; JavaScript that is executed whenever
the slider is done moving
- ``steps`` - an integer; number of steps from minimum to maximum
value
- ``default_l`` - an integer (default: 0); the default position of
the left edge of the slider
- ``default_r`` - an integer (default: 1); the default position of
the right edge of the slider
- ``margin`` - an integer (default: 0); size of margin to insert
around the slider
OUTPUT:
- a string - HTML format
EXAMPLES:
We create a jQuery range slider. If you do the following in the
notebook you should obtain a slider that when moved pops up a
window showing its current position::
sage: from sagenb.notebook.interact import html_rangeslider, html
sage: html(html_rangeslider('slider-007', 'null', 'alert(pos[0]+", "+pos[1])', steps=5, default_l=2, default_r=3, margin=5))
<html>...slider...range...</html>
"""
val_html = ''
if values != 'null':
val_html = """
<tr>
<td>
<font color="black" id="%s-lbl"></font>
</td>
</tr>""" % id
s = """
<table>
<tr>
<td>
<div id="%s" style="margin:%spx; margin-left: 1.0em; margin-right: 1.0em; width: 20.0em;"></div>
</td>
</tr>
%s
</table>
""" % (id, int(margin), val_html)
# We now generate javascript that gets run after the above div
# gets inserted. This happens because of the setTimeout function
# below which gets passed an anonymous function.
s += """<script>
(function() {
var values = %s, pos = [%s, %s], sel = '#%s', updatePos;
updatePos = function() {
pos[0] = $(sel).slider('values', 0);
pos[1] = $(sel).slider('values', 1);
if (values != null) {
$(sel + '-lbl').text('(' + values[pos[0]] + ', ' +
values[pos[1]] + ')');
}
};
setTimeout(function() {
$(sel).slider({
range: true,
step: 1,
min: 0,
max: %s,
values: [%s, %s],
change: function (e, ui) {
updatePos();
%s;
},
slide: updatePos
});
updatePos();
}, 1);
})();
</script>""" % (values, default_l, default_r, id, steps - 1,
default_l, default_r, callback)
# Note: Change 'change' to 'slide' and it changes the slider every
# time it moves; needs much more work to actually work, since
# server gets flooded by requests.
return s
def html_color_selector(id, change, input_change, default='000000',
widget='colorpicker', hide_box=False):
"""
Return HTML representation of a jQuery color selector.
INPUT:
- ``id`` - an integer or string; an identifier (e.g., cell ID) for
this selector
- ``change`` - a string; JavaScript code to execute when the color
selector changes.
- ``default`` - a string (default: ``'000000'``); default color as
a 6-character HTML hexadecimal string.
- ``widget`` - a string (default: 'colorpicker'); the color
selector widget to use; choices are 'colorpicker'; in Debian other
values are not supported.
- ``hide_box`` - a boolean (default: False); whether to hide the
input box associated with the color selector widget
OUTPUT:
- a string - HTML that creates the slider.
EXAMPLES::
sage: sagenb.notebook.interact.html_color_selector(0, 'alert("changed")', '', default='0afcac')
'...<table>...colorpicker...'
"""
input_style = ''
if hide_box:
input_style = 'display: none;'
if widget == 'farbtastic':
raise ValueError("not supported in Debian")
elif widget == 'colorpicker':
# HTML
select = 'background: url(/javascript/jquery/plugins/colorpicker/images/select.png)'
style1 = 'position: relative; width: 36px; height: 36px; %s;' % select
style2 = 'position: absolute; top: 3px; left: 3px; width: 30px; height: 30px; %s center; background-color: %s' % (select, default)
s = """
<table>
<tr>
<td>
<input type="text"
id="%s"
name="color"
onchange="%s"
value="%s"
style="%s" />
</td>
</tr>
</table>""" % (id, input_change, default, input_style)
# JS
s += """<script>
setTimeout(function () {
var def = '%s'.slice(1), input = $('#%s');
var update = function(hex) {
input.css({
backgroundColor: '#'+hex,
// Should be good enough:
color: (parseInt(hex.slice(0, 2), 16) + parseInt(hex.slice(2, 4), 16) + parseInt(hex.slice(4, 6), 16)) / 3 > 127 ? '#000000' : '#ffffff'
});
};
update(def);
input.colorpicker({
color: '%s',
alpha: true,
showOn: 'all',
buttonImage: '/javascript/jquery/plugins/colorpicker/images/ui-colorpicker.png',
buttonImageOnly: true,
buttonColorize: true,
select: function(event, color) {
var hex = color.formatted;
var hsb = color.colorPicker.color.getHSV();
hsb.b = hsb.v; // compatibility with old colorpicker
var rgb = color.colorPicker.color.getRGB();
update(hex);
%s;
},
});
}, 1);
</script>""" % (default, id, default, change)
elif widget == 'jpicker':
raise ValueError("not supported in Debian")
return s
class InteractElement(object):
def label(self):
"""
Returns an empty label for this element. This should be
overridden for subclasses that need a label.
OUTPUT:
- a string
EXAMPLES::
sage: from sagenb.notebook.interact import UpdateButton, InteractElement
sage: b = UpdateButton(1, 'autoupdate')
sage: isinstance(b, InteractElement)
True
sage: b.label()
''
"""
return ""
def set_canvas(self, canvas):
"""
Sets the :class:`InteractCanvas` on which this element
appears. This method is primarily called in the constructor
for :class:`InteractCanvas`.
EXAMPLES::
sage: from sagenb.notebook.interact import InputBox, InteractCanvas
sage: B = InputBox('x',2)
sage: canvas1 = InteractCanvas([B], 3)
sage: canvas2 = InteractCanvas([B], 3)
sage: B.canvas() is canvas2
True
sage: B.set_canvas(canvas1)
sage: B.canvas() is canvas1
True
"""
self._canvas = canvas
def canvas(self):
"""
Returns the :class:`InteractCanvas` associated to this
element. If no canvas has been set (via the
:meth:`set_canvas` method), then raise a ValueError.
EXAMPLES::
sage: from sagenb.notebook.interact import InputBox, InteractCanvas
sage: B = InputBox('x',2)
sage: canvas1 = InteractCanvas([B], 3)
sage: canvas2 = InteractCanvas([B], 3)
sage: B.canvas() is canvas2
True
"""
if hasattr(self, '_canvas'):
return self._canvas
else:
raise ValueError("this element does not have a canvas associated with it")
class InteractControl(InteractElement):
def __init__(self, var, default_value, label=None):
"""
Abstract base class for :func:`interact` controls. These are
controls that are used in a specific :func:`interact`. They
have internal state information about the specific function
being interacted, etc.
INPUT:
- ``var`` - a string; name of variable that this control
interacts
- ``default_value`` - the default value of the variable
corresponding to this control.
- ``label`` - a string (default: None); label for this
control; if None then defaults to ``var``.
EXAMPLES::
sage: from sagenb.notebook.interact import InteractControl
sage: InteractControl('x', default_value=5)
A InteractControl (abstract base class)
"""
self.__var = var
self.__cell_id = SAGE_CELL_ID
self.__default_value = default_value
self.__adapt_number = new_adapt_number()
if label is None:
self.__label = var
else:
self.__label = label
InteractElement.__init__(self)
def __repr__(self):
"""
String representation of :func:`interact` control.
OUTPUT:
- a string
EXAMPLES::
sage: from sagenb.notebook.interact import InteractControl
sage: InteractControl('x', default_value=5).__repr__()
'A InteractControl (abstract base class)'
"""
return "A InteractControl (abstract base class)"
def value_js(self):
"""
JavaScript that when evaluated gives the current value of this
control. This should be redefined in a derived class.
OUTPUT:
- a string - defaults to 'NULL' - this should be redefined.
EXAMPLES::
sage: sagenb.notebook.interact.InteractControl('x', default_value=5).value_js()
'NULL'
"""
return 'NULL'
def label(self):
"""
Return the text label of this :func:`interact` control.
OUTPUT:
- a string
EXAMPLES::
sage: from sagenb.notebook.interact import InteractControl
sage: InteractControl('x', default_value=5, label='the x value').label()
'the x value'
"""
return self.__label
def default_value(self):
"""
Return the default value of the variable corresponding to this
:func:`interact` control.
OUTPUT:
- an object
EXAMPLES::
sage: from sagenb.notebook.interact import InteractControl
sage: InteractControl('x', 19/3).default_value()
19/3
"""
return self.__default_value
def html_escaped_default_value(self):
"""
Returns the HTML escaped default value of the variable
corresponding to this :func:`interact` control. Note that any
HTML that uses quotes around this should use double quotes and
not single quotes.
OUTPUT:
- a string
EXAMPLES::
sage: from sagenb.notebook.interact import InteractControl
sage: InteractControl('x', '"cool"').html_escaped_default_value()
'"cool"'
sage: InteractControl('x',"'cool'").html_escaped_default_value()
"'cool'"
sage: x = var('x')
sage: InteractControl('x', x^2).html_escaped_default_value()
'x^2'
"""
s = self.default_value()
if not isinstance(s, str):
s = repr(s)
import cgi
return cgi.escape(s, quote=True)
def adapt_number(self):
"""
Return integer index into adapt dictionary of function that is
called to adapt the values of this control to Python.
OUTPUT:
- an integer
EXAMPLES::
sage: from sagenb.notebook.interact import InteractControl
sage: InteractControl('x', 19/3).adapt_number() # random -- depends on call order
2
"""
return self.__adapt_number
def _adaptor(self, value, globs):
"""
Adapt a user input, which is a string, to be an element selected
by this control.
INPUT:
- ``value`` - the string the user typed in
- ``globs`` - a string:object dictionary; the globals
interpreter variables, e.g., :func:`globals`, which is
useful for evaluating value.
OUTPUT:
- an object
EXAMPLES::
sage: sagenb.notebook.interact.InteractControl('x', 1)._adaptor('2/3', globals())
2/3
"""
return sage_eval(value, globs)
def interact(self, *args):
r"""
Return a string that when evaluated in JavaScript calls the
JavaScript :func:`interact` function with appropriate inputs for
this control.
This method will check to see if there is a canvas attached to
this control and whether or not controls should automatically
update the output when their values change. If no canvas is
associated with this control, then the control will
automatically update.
OUTPUT:
- a string - that is meant to be evaluated in JavaScript
EXAMPLES::
sage: sagenb.notebook.interact.InteractControl('x', 1).interact()
"...interact...x...1..."
"""
# We have to do a try/except block here since the control may
# not have a canvas associated with it.
try:
auto_update = self.canvas().is_auto_update()
except ValueError:
auto_update = True
recompute = 0
if auto_update:
recompute = 1
update = "{variable: '%s', adapt_number: %s, value: encode64(%s)}" % (
self.var(), self.adapt_number(), self.value_js(*args))
return "interact(%r, %s, %s)" % (self.cell_id(), update, recompute)
def var(self):
"""
Return the name of the variable that this control interacts.
OUTPUT:
- a string - name of a variable as a string.
EXAMPLES::
sage: sagenb.notebook.interact.InteractControl('theta', 1).var()
'theta'
"""
return self.__var
def cell_id(self):
"""
Return the ID of the cell that contains this :func:`interact` control.
OUTPUT:
- an integer or a string
EXAMPLES:
The output below should equal the ID of the current cell::
sage: sagenb.notebook.interact.InteractControl('theta', 1).cell_id()
0
"""
return self.__cell_id
class InputBox(InteractControl):
def __init__(self, var, default_value, label=None, type=None, width=80, height = 1, **kwargs):
"""
An input box :func:`interact` control.
INPUT:
- ``var`` - a string; name of variable that this control
interacts
- ``default_value`` - the default value of the variable
corresponding to this control
- ``label`` - a string (default: None); label for this
control
- ``type`` - a type (default: None); the type of this control,
e.g., the type 'bool'
- ``height`` - an integer (default: 1); the number of rows.
If greater than 1 a value won't be returned until something
outside the textarea is clicked.
- ``width`` - an integer (default: 80); the character width of
this control
- ``kwargs`` - a dictionary; additional keyword options
EXAMPLES::
sage: sagenb.notebook.interact.InputBox('theta', 1, 'theta')
An InputBox interactive control with theta=1 and label 'theta'
sage: sagenb.notebook.interact.InputBox('theta', 1, 'theta', int)
An InputBox interactive control with theta=1 and label 'theta'
"""
InteractControl.__init__(self, var, default_value, label)
self.__type = type
self.__width = width
self.__height = height
self._kwargs = kwargs
def __repr__(self):
"""
String representation of an :class:`InputBox` interactive control.
OUTPUT:
- a string
EXAMPLES::
sage: sagenb.notebook.interact.InputBox('theta', 1).__repr__()
"An InputBox interactive control with theta=1 and label 'theta'"
"""
return 'An InputBox interactive control with %s=%r and label %r' % (
self.var(), self.default_value(), self.label())
def _adaptor(self, value, globs):
"""
Adapt a user input, which is the text they enter, to be an
element selected by this control.
INPUT:
- ``value`` - text entered by user
- ``globs`` - a string:object dictionary; the :func:`globals`
interpreter variables (not used here).
OUTPUT:
- an object
EXAMPLES::
sage: sagenb.notebook.interact.InputBox('theta', Color('red'), type=Color)._adaptor('#aaaaaa',globals())
RGB color (0.66..., 0.66..., 0.66...)
"""
if self.__type is None:
return sage_eval(value, globs)
elif self.__type is str:
return value
elif self.__type is Color:
try:
return Color(value)
except ValueError:
try:
return Color('#' + value)
except ValueError:
print("Invalid color '%s', using default Color()" % value)
return Color()
else:
return self.__type(sage_eval(value, globs))
def value_js(self):
"""
Return JavaScript string that will give the value of this
control element.
OUTPUT:
- a string - JavaScript
EXAMPLES::
sage: sagenb.notebook.interact.InputBox('theta', 1).value_js()
'this.value'
"""
if self.__type is bool:
return 'this.checked'
else:
return 'this.value'
def render(self):
r"""
Render this control as a string.
OUTPUT:
- a string - HTML format
EXAMPLES::
sage: sagenb.notebook.interact.InputBox('theta', 1).render()
'...input...value="1"...theta...'
"""
if self.__type is bool:
return """<input type="checkbox" %s width=200px onchange="%s"></input>"""%(
'checked' if self.default_value() else '', self.interact())
elif self.__type is str and self.__height ==1:
return """<input type="text" value="%s" size=%s onchange="%s"></input>"""%(
self.html_escaped_default_value(), self.__width, self.interact())
elif self.__type is str and self.__height > 1:
textval = self.html_escaped_default_value()
return """<textarea type="text" value="%s" rows="%s" cols="%s" onchange="%s">%s</textarea>"""%(
textval, self.__height, self.__width, self.interact(), textval)
else:
return """<input type="text" value="%s" size=%s onchange="%s"></input>"""%(
self.html_escaped_default_value(), self.__width, self.interact())
class ColorInput(InputBox):
def value_js(self, n):
"""
Return JavaScript that evaluates to value of this control.
If ``n`` is 0, return code for evaluation by the actual color
control. If ``n`` is 1, return code for the text area that
displays the current color.
INPUT:
- ``n`` - integer, either 0 or 1.
OUTPUT:
- a string
EXAMPLES::
sage: C = sagenb.notebook.interact.ColorInput('c', Color('red'))
sage: C.value_js(0)
'color'
sage: C.value_js(1)
'this.value'
"""
if n == 0:
return 'color'
else:
return 'this.value'
def render(self):
"""
Render this color input box to HTML.
OUTPUT:
- a string - HTML format
EXAMPLES::
sage: sagenb.notebook.interact.ColorInput('c', Color('red')).render()
'...table...color...'
"""
return html_color_selector('color-selector-%s-%s' % (self.var(),
self.cell_id()),
change=self.interact(0),
input_change=self.interact(1),
default=self.default_value().html_color(),
**self._kwargs)
class InputGrid(InteractControl):
def __init__(self, var, rows, columns, default_value=None, label=None,
to_value=lambda x: x, width=4):
"""
A grid interact control.
INPUT:
- ``var`` - an object; the variable
- ``rows`` - an integer; the number of rows
- ``columns`` - an integer; the number of columns
- ``default_value`` - an object; if this is a scalar, it is
put in every cell; if it is a list, it is filled into the
cells row by row; if it is a nested list, then it is filled
into the cells according to the nesting structure.
- ``label`` - a string; the label for the control
- ``to_value`` - a function applied to the nested list from
user input when assigning the variable
- ``width`` - an integer; the width of the input boxes
EXAMPLES::
sage: sagenb.notebook.interact.InputGrid('M', 2,2, default_value = 0, label='M')
A 2 x 2 InputGrid interactive control with M=[[0, 0], [0, 0]] and label 'M'
sage: sagenb.notebook.interact.InputGrid('M', 2,2, default_value = [[1,2],[3,4]], label='M')
A 2 x 2 InputGrid interactive control with M=[[1, 2], [3, 4]] and label 'M'
sage: sagenb.notebook.interact.InputGrid('M', 2,2, default_value = [[1,2],[3,4]], label='M', to_value=MatrixSpace(ZZ,2,2))
A 2 x 2 InputGrid interactive control with M=[1 2]
[3 4] and label 'M'
sage: sagenb.notebook.interact.InputGrid('M', 1, 3, default_value=[1,2,3], to_value=lambda x: vector(flatten(x)))
A 1 x 3 InputGrid interactive control with M=(1, 2, 3) and label 'M'
"""
self.__rows = rows
self.__columns = columns
self.__to_value = to_value
self.__width = width
if type(default_value) != list:
default_value = [[default_value for _ in range(columns)] for _ in range(rows)]
elif not all(type(elt) == list for elt in default_value):
default_value = [[default_value[i * columns + j] for j in xrange(columns)] for i in xrange(rows)]
self.__default_value_grid = default_value
InteractControl.__init__(self, var, self.__to_value(default_value), label)
def __repr__(self):
"""
String representation of an :class:`InputGrid` interactive control.
OUTPUT:
- a string
EXAMPLES::
sage: sagenb.notebook.interact.InputGrid('M', 2,2).__repr__()
"A 2 x 2 InputGrid interactive control with M=[[None, None], [None, None]] and label 'M'"
"""
return 'A %r x %r InputGrid interactive control with %s=%r and label %r' % (
self.__rows, self.__columns, self.var(), self.default_value(),
self.label())
def _adaptor(self, value, globs):
"""
Adapt a user input, which is the text they enter, to be an
element selected by this control.
INPUT:
- ``value`` - text entered by user
- ``globs`` - a string:object dictionary; the :func:`globals`
interpreter variables (not used here).
OUTPUT:
- an object
EXAMPLES::
sage: sagenb.notebook.interact.InputGrid('M', 1,3, default_value=[[1,2,3]], to_value=lambda x: vector(flatten(x)))._adaptor("[[4,5,6]]", globals())
(4, 5, 6)
"""
return self.__to_value(sage_eval(value, globs))
def value_js(self):
"""
Return JavaScript string that will give the value of this
control element.
OUTPUT:
- string - JavaScript
EXAMPLES::
sage: sagenb.notebook.interact.InputGrid('M', 2,2).value_js()
"...jQuery...table...map...val...join..."
"""
# Basically, given an input element in a table, it constructs
# a python string representation of a list of lists from the
# rows in the table.
return """
'[[' +
jQuery(this).parents('table').eq(0).find('tr').map(function() {
return jQuery(this).find('input').map(function() {
return jQuery(this).val();
}).get().join(',');
}).get().join('],[') +
']]' """
def render(self):
"""
Render this control as a string.
OUTPUT:
- string - HTML format
EXAMPLES::
sage: sagenb.notebook.interact.InputGrid('M', 1,2).render()
'...table...input...M...'
"""
s = "<table>"
for i in range(self.__rows):
s += " <tr>"
for j in range(self.__columns):
s += ' <td>\n'
s += ' <input type="text" value="%r" size="%s" onchange="%s" />\n' % (self.__default_value_grid[i][j], self.__width, self.interact())
s += ' </td>\n'
s += ' </tr>\n'
s += '</table>\n'
return s
class Selector(InteractControl):
def __init__(self, var, values, label=None, default=0,
nrows=None, ncols=None, width=None, buttons=False):
"""
A drop down menu or a button bar that when pressed sets a
variable to a given value.
INPUT:
- ``var`` - a string; variable name
- ``values`` - a list; button values
- ``label`` - a string (default: None); label off to the left
for this button group
- ``default`` - an integer (default: 0); position of default
value in values list.
- ``nrows`` - an integer (default: None); number of rows
- ``ncols`` - an integer (default: None); number of columns
- ``width`` - an integer (default: None); width of all the
buttons
- ``buttons`` - a bool (default: False); if True use buttons
instead of dropdown
EXAMPLES::
sage: sagenb.notebook.interact.Selector('x', [1..5], 'alpha', default=2)
Selector with 5 options for variable 'x'
sage: sagenb.notebook.interact.Selector('x', [1..4], 'alpha', default=2, nrows=2, ncols=2, width=10, buttons=True)
Selector with 4 options for variable 'x'
"""
if (len(values) > 0 and isinstance(values[0], tuple) and
len(values[0]) == 2):
vals = [z[0] for z in values]
lbls = [repr(z[1]) if z[1] is not None else None for z in values]
else:
vals = values
lbls = [None] * len(vals)
default = int(default)
if default < 0 or default >= len(vals):
default = 0
InteractControl.__init__(self, var, vals[default], label)
self.__default = default
self.__buttons = buttons
self.__values = vals
self.__labels = lbls
if nrows is None:
if ncols is not None:
nrows = len(values) // ncols
if ncols * nrows < len(values):
nrows += 1
else:
nrows = 1 # temporary
else:
nrows = int(nrows)
if nrows <= 0:
nrows = 1
if ncols is None:
ncols = len(values) // nrows
if ncols * nrows < len(values):
ncols += 1
self.__nrows = nrows
self.__ncols = ncols
if width is not None:
self.__width = "width:%sex;" % width
else:
self.__width = ''
self.__selected = 'background-color:orange;'
def __repr__(self):
"""
String representation of a :class:`Selector` interactive control.
OUTPUT:
- a string
EXAMPLES::
sage: sagenb.notebook.interact.Selector('x', [1..5]).__repr__()
"Selector with 5 options for variable 'x'"
"""
return "Selector with %s options for variable '%s'" % (
len(self.__values), self.var())
def _adaptor(self, value, globs):
"""
Adapt value of button or menu selection. The button value is
just an integer, and this function adapts it to be the value
that we associate with that button.
INPUT:
- ``value` - an integer; value sent in via JavaScript
- ``globs`` - a string:object dictionary; the :func:`globals`
interpreter variables (not used here).
OUTPUT:
- an object
EXAMPLES::
sage: S = sagenb.notebook.interact.Selector('x', ['first',x^3+5])
sage: S._adaptor(0,globals())
'first'
sage: S._adaptor(1,globals())
x^3 + 5
"""
return self.__values[int(value)]
def use_buttons(self):
"""
Whether or not to use buttons instead of a drop down menu for
this select list.
OUTPUT:
- a bool
EXAMPLES::
sage: sagenb.notebook.interact.Selector('x', [1..5]).use_buttons()
False
sage: sagenb.notebook.interact.Selector('x', [1..5], buttons=True).use_buttons()
True
"""
return self.__buttons
def value_js(self):
"""
Return JavaScript string that will give the value of this
control element.
OUTPUT:
- a string - JavaScript
EXAMPLES::
sage: sagenb.notebook.interact.Selector('x', [1..5]).value_js()
'this.options[this.selectedIndex].value'
sage: sagenb.notebook.interact.Selector('x', [1..5], buttons=True).value_js()
'this.value'
"""
if self.use_buttons():
return 'this.value'
else:
# Now we have to use a option selector.
return 'this.options[this.selectedIndex].value'
def render(self):
"""
Render this control as a string.
OUTPUT:
- a string - HTML format
EXAMPLES::
sage: sagenb.notebook.interact.Selector('x', [1..5]).render()
'...select...x...'
sage: sagenb.notebook.interact.Selector('x', [1..5], buttons=True).render()
'...table...button...x...'
"""
width = self.__width
vals = self.__values
lbls = self.__labels
default = self.__default
label = self.label()
use_buttons = self.use_buttons()
event = self.interact()
if use_buttons:
#On selected buttons, border is set to inset, on
#unselected boxes - outset. This usually is default
#rendering.
if len(vals) > 1:
event = """$('BUTTON', this.parentNode).css('border-style', 'outset'); $(this).css('border-style', 'inset'); %s""" % event
s = """<table style="border:1px solid #dfdfdf; background-color:#efefef;">"""
else:
s = """<select onchange="%s;">""" % event
i = 0
for r in range(self.__nrows):
if use_buttons:
s += '\n<tr><td>'
for c in range(self.__ncols):
if i >= len(vals):
i += 1
continue
style = width
#if i == default:
# style += self.__selected
if lbls[i] is None:
if isinstance(vals[i], str):
lbl = vals[i]
else:
lbl = repr(vals[i])
else:
lbl = lbls[i]
if use_buttons:
s += """<button style="%s%s" value="%s" onclick="%s">%s</button>\n""" % ('border-style:inset;' if i == default and len(vals) > 1 else 'border-style:outset;', style, i, event, lbl)
else:
s += '<option value="%s" %s>%s</option>\n' % (i, 'selected' if i == default else '', lbl)
i += 1
if use_buttons:
s += '</td></tr>'
if use_buttons:
s += '</table>'
else:
s += '</select>'
return s
class SliderGeneric(InteractControl):
def __init__(self, var, values, default_value, label=None,
display_value=True):
"""
An abstract slider :func:`interact` control that takes on the
given list of values.
INPUT:
- ``var`` - a string; name of variable being interacted
- ``values`` - a list; a list of the values that the slider
will take on
- ``default_value`` - an object; default value of the slider.
- ``label`` - a string; alternative label to the left of the
slider, instead of the variable.
- ``display_value`` - a bool; whether to display the current
value on the slider
EXAMPLES::
sage: sagenb.notebook.interact.SliderGeneric('x', [1..5], 2, 'alpha')
Abstract Slider Interact Control: alpha [1--|2|---5]
"""
InteractControl.__init__(self, var, default_value, label=label)
self.__values = values
self.__display_value = display_value
def __repr__(self):
"""
Return string representation of this slider control.
OUTPUT:
- a string
EXAMPLES::
sage: sagenb.notebook.interact.SliderGeneric('x', [1..5], 2, 'alpha').__repr__()
'Abstract Slider Interact Control: alpha [1--|2|---5]'
"""
return "Abstract Slider Interact Control: %s [%s--|%s|---%s]" % (
self.label(), self.__values[0],
self.default_value(), self.__values[-1])
def values(self):
"""
Return list of values the slider acts on.
OUTPUT:
- a list
EXAMPLES::
sagenb.notebook.interact.Slider('x', [1..5], 2, 'alpha').values()
[1, 2, 3, 4, 5]
"""
return self.__values
def display_value(self):
"""
Returns whether to display the value on the slider.
OUTPUT:
- a bool
EXAMPLES::
sagenb.notebook.interact.Slider('x', [1..5], 2, 'alpha').display_value()
True
"""
return self.__display_value
def values_js(self):
"""
Returns JavaScript array representation of values or 'null' if
display_value=False
OUTPUT:
- a string
EXAMPLES::
sage: sagenb.notebook.interact.Slider('x', [1..5], 2, 'alpha').values_js()
'["1","2","3","4","5"]'
sage: sagenb.notebook.interact.Slider('x', [1..5], 2, 'alpha', False).values_js()
'null'
sage: sagenb.notebook.interact.Slider('x', [pi..2*pi], 2, 'alpha').values_js()
'["pi","pi + 1","pi + 2","pi + 3"]'
"""
if self.__display_value == False:
return "null"
s = "["
for i in self.__values:
ie = repr(i).replace("\\", "\\\\").replace("\"", "\\\"").replace("'", "\\'")
s += "\"%s\"," % ie
s = s[:-1] + ']'
return s
class Slider(SliderGeneric):
def __init__(self, var, values, default_position, label=None,
display_value=True):
"""
A slider :func:`interact` control that takes on the given list of
values.
INPUT:
- ``var`` - a string; name of variable being interacted
- ``values`` - a list; a list of the values that the slider
will take on
- ``default_position`` - an integer; default location that the
slider is set to.
- ``label`` - a string; alternative label to the left of the
slider, instead of the variable.
- ``display_value`` - a bool, whether to display the current
value right of the slider
EXAMPLES::
sage: sagenb.notebook.interact.Slider('x', [1..5], 2, 'alpha')
Slider Interact Control: alpha [1--|3|---5]
"""
SliderGeneric.__init__(self, var, values, values[default_position],
label=label, display_value=display_value)
self.__default_position = default_position
def __repr__(self):
"""
Return string representation of this slider control.
OUTPUT:
- a string
EXAMPLES::
sage: sagenb.notebook.interact.Slider('x', [1..5], 2, 'alpha').__repr__()
'Slider Interact Control: alpha [1--|3|---5]'
"""
return "Slider Interact Control: %s [%s--|%s|---%s]" % (
self.label(), self.values()[0],
self.default_value(), self.values()[-1])
def default_position(self):
"""
Return the default position (as an integer) of the slider.
OUTPUT:
- an integer
EXAMPLES::
sage: sagenb.notebook.interact.Slider('x', [1..5], 2, 'alpha').default_position()
2
"""
return self.__default_position
def value_js(self):
"""
Return JavaScript string that will give the value of this
control element.
OUTPUT:
- a string - JavaScript
EXAMPLES::
sage: sagenb.notebook.interact.Slider('x', [1..5], 2, 'alpha').value_js()
'position'
"""
return "position"
def _adaptor(self, position, globs):
"""
Adapt a user input, which is the slider position, to be an
element selected by this control.
INPUT:
- ``position`` - an object; position of the slider
- ``globs`` - a string:object dictionary; the :func:`globals`
interpreter variables (not used here).
OUTPUT:
- an object
EXAMPLES::
sage: sagenb.notebook.interact.Slider('x', [1..5], 2, 'alpha')._adaptor(2,globals())
3
"""
v = self.values()
# We have to cast to int, since it comes back as a float that
# is too big.
return v[int(position)]
def render(self):
"""
Render this control as an HTML string.
OUTPUT:
- a string - HTML format
EXAMPLES::
sage: sagenb.notebook.interact.Slider('x', [1..5], 2, 'alpha').render()
'...table...slider...["1","2","3","4","5"]...'
sage: sagenb.notebook.interact.Slider('x', [1..5], 2, 'alpha', display_value=False).render()
'...table...slider...null...'
"""
return html_slider('slider-%s-%s'%(self.var(), self.cell_id()),
self.values_js(), self.interact(),
steps=len(self.values()),
default=self.default_position())
class RangeSlider(SliderGeneric):
def __init__(self, var, values, default_position, label=None,
display_value=True):
"""
A range slider :func:`interact` control that takes on the given
list of values.
INPUT:
- ``var`` - a string; name of variable being interacted
- ``values`` - a list; a list of the values that the slider
will take on
- ``default_position`` - an integer 2-tuple; default location
that the slider is set to.
- ``label`` - a string; alternative label to the left of the
slider, instead of the variable.
- ``display_value`` - a bool, whether to display the current
value below the slider
EXAMPLES::
sage: sagenb.notebook.interact.RangeSlider('x', [1..5], (2,3), 'alpha')
Range Slider Interact Control: alpha [1--|3==4|---5]
"""
SliderGeneric.__init__(self, var, values, (values[default_position[0]], values[default_position[1]]), label=label, display_value=display_value)
self.__default_position = default_position
def __repr__(self):
"""
Return string representation of this slider control.
OUTPUT:
- a string
EXAMPLES::
sage: sagenb.notebook.interact.RangeSlider('x', [1..5], (2,3), 'alpha').__repr__()
'Range Slider Interact Control: alpha [1--|3==4|---5]'
"""
return "Range Slider Interact Control: %s [%s--|%s==%s|---%s]" % (
self.label(), self.values()[0],
self.default_value()[0], self.default_value()[1], self.values()[-1])
def default_position(self):
"""
Return the default position (as an integer) of the slider.
OUTPUT:
- an integer 2-tuple
EXAMPLES::
sage: sagenb.notebook.interact.RangeSlider('x', [1..5], (2,3), 'alpha').default_position()
(2, 3)
"""
return self.__default_position
def value_js(self):
"""
Return JavaScript string that will give the value of this
control element.
OUTPUT:
- a string - JavaScript
EXAMPLES::
sage: sagenb.notebook.interact.RangeSlider('x', [1..5], (2,3), 'alpha').value_js()
"pos[0]+' '+pos[1]"
"""
return "pos[0]+' '+pos[1]"
def _adaptor(self, position, globs):
"""
Adapt a user input, which is the slider position, to be an
element selected by this control.
INPUT:
- ``position`` - an object; position of the slider
- ``globs` - a string:object dictionary; the :func:`globals`
interpreter variables (not used here).
OUTPUT:
- an object
EXAMPLES::
sage: sagenb.notebook.interact.RangeSlider('x', [1..5], (2,3), 'alpha')._adaptor("2 3",globals())
(3, 4)
"""
v = self.values()
s = position.split(' ')
# Use of int() here matches it's use in Slider._adaptor
return (v[int(s[0])], v[int(s[1])])
def render(self):
"""
Render this control as an HTML string.
OUTPUT:
- string - HTML format
EXAMPLES::
sage: sagenb.notebook.interact.RangeSlider('x', [1..5], (2,3), 'alpha').render()
'...table...slider...["1","2","3","4","5"]...range...'
sage: sagenb.notebook.interact.RangeSlider('x', [1..5], (2,3), 'alpha', display_value=False).render()
'...table...slider...null...range...
"""
return html_rangeslider('slider-%s-%s'%(self.var(), self.cell_id()),
self.values_js(), self.interact(),
steps=len(self.values()),
default_l=self.default_position()[0],
default_r=self.default_position()[1])
class TextControl(InteractControl):
def __init__(self, var, data):
"""
A text field :func:`interact` control
INPUT:
- ``data`` - a string; the HTML value of the text field
EXAMPLES::
sage: sagenb.notebook.interact.TextControl('x', 'something')
Text Interact Control: something
"""
InteractControl.__init__(self, var, data, label='')
self.__data = data
def __repr__(self):
"""
Return string representation of this control.
OUTPUT:
- a string
EXAMPLES::
sage: sagenb.notebook.interact.TextControl('x', 'something').__repr__()
'Text Interact Control: something'
"""
return 'Text Interact Control: %s' % self.default_value()
def render(self):
"""
Render this control as an HTML string.
OUTPUT:
- a string - HTML format
EXAMPLES::
sage: sagenb.notebook.interact.TextControl('x', 'something').render()
'...div...something...'
"""
return '<div style="color:black; padding-bottom:5px">%s</div>' % self.default_value()
class InteractCanvas(object):
def __init__(self, controls, id, layout=None, width=None, **options):
"""
Base class for :func:`interact` canvases. This is where all
the controls along with the output of the interacted function
are laid out and rendered.
INPUT:
- ``controls`` - a list of :class:`InteractControl` instances.
- ``id`` - an integer or a string; the ID of the cell that
contains this InteractCanvas.
- ``layout`` - a dictionary with keys
'top','bottom','left','right' and values lists of rows of
control variable names. If a dictionary is not passed in,
then the value of layout is set to the 'top' value. If
``None``, then all control names are put on separate rows in
the 'top' value.
- ``width`` - the width of the interact control
- ``options`` - any additional keyword arguments (for example,
auto_update=False)
EXAMPLES::
sage: B = sagenb.notebook.interact.InputBox('x',2)
sage: sagenb.notebook.interact.InteractCanvas([B], 3)
Interactive canvas in cell 3 with 1 controls
"""
for control in controls:
control.set_canvas(self)
self.__controls = controls
self.__cell_id = id
self.__width = width
self.__options = options
if layout is None:
layout = [[c.var()] for c in self.__controls]
if not isinstance(layout, dict):
layout={'top': layout}
self.__layout = layout
def __repr__(self):
"""
Print representation of an interactive canvas.
OUTPUT:
- a string
EXAMPLES::
sage: B = sagenb.notebook.interact.InputBox('x',2)
sage: sagenb.notebook.interact.InteractCanvas([B], 3).__repr__()
'Interactive canvas in cell 3 with 1 controls'
"""
return "Interactive canvas in cell %s with %s controls" % (
self.__cell_id, len(self.__controls))
def cell_id(self):
"""
Return the ID of the cell that contains this :func:`interact` control.
OUTPUT:
- an integer or a string
EXAMPLES:
The output below should equal the ID of the current cell::
sage: B = sagenb.notebook.interact.InputBox('x',2)
sage: C = sagenb.notebook.interact.InteractCanvas([B], 3); C
Interactive canvas in cell 3 with 1 controls
sage: C.cell_id()
3
"""
return self.__cell_id
def is_auto_update(self):
r"""
Returns True if any change of the values for the controls on
this canvas should cause an update. If ``auto_update=False`` was
not specified in the constructor for this canvas, then this
will default to True.
OUTPUT:
- a bool
EXAMPLES::
sage: B = sagenb.notebook.interact.InputBox('x',2)
sage: canvas = sagenb.notebook.interact.InteractCanvas([B], 3)
sage: canvas.is_auto_update()
True
sage: canvas = sagenb.notebook.interact.InteractCanvas([B], 3, auto_update=False)
sage: canvas.is_auto_update()
False
"""
return self.__options.get('auto_update', True)
def controls(self):
"""
Return a list of controls in this canvas.
OUTPUT:
- list of controls
.. note::
Returns a reference to a mutable list.
EXAMPLES::
sage: B = sagenb.notebook.interact.InputBox('x',2)
sage: sagenb.notebook.interact.InteractCanvas([B], 3).controls()
[An InputBox interactive control with x=2 and label 'x']
"""
return self.__controls
def render_output(self):
"""
Render in text (HTML) form the output portion of the
:func:`interact` canvas.
The output contains two special tags, <?TEXT> and <?HTML>,
which get replaced at runtime by the text and HTML parts
of the output of running the function.
OUTPUT:
- a string - HTML format
EXAMPLES::
sage: B = sagenb.notebook.interact.InputBox('x',2)
sage: sagenb.notebook.interact.InteractCanvas([B], 3).render_output()
'...div...interact...3...'
"""
return """
<div id="cell-interact-{0}">{1}
<table border=0 bgcolor="white" width=100%>
<tr>
<td bgcolor="white" align=left valign=top>
<pre>{2}</pre>
</td>
</tr>
<tr>
<td align=left valign=top>{3}</td>
</tr>
</table>{4}
</div>""".format(self.cell_id(), INTERACT_START, INTERACT_TEXT,
INTERACT_HTML, INTERACT_END)
def render_controls(self, side='top'):
"""
Render in text (HTML) form all the input controls.
OUTPUT:
- a string - HTML format
EXAMPLES::
sage: B = sagenb.notebook.interact.InputBox('x',2)
sage: sagenb.notebook.interact.InteractCanvas([B], 3).render_controls()
'...table...x...input...2...'
"""
if side not in self.__layout:
return ''
tbl_body = ''
controls = dict([c.var(), c] for c in self.__controls)
for row in self.__layout[side]:
tbl_body += '<tr>'
for c_name in row:
c = controls[c_name]
if c.label() == '':
tbl_body += '<td colspan=2>{0}</td>\n'.format(c.render())
else:
tbl_body += '<td align=right><font color="black">{0} </font></td><td>{1}</td>\n'.format(c.label(), c.render())
tbl_body += '</tr>'
return '<table>' + tbl_body + '</table>'
def wrap_in_outside_frame(self, inside):
"""
Return the entire HTML for the interactive canvas, obtained by
wrapping all the inside HTML of the canvas in a div and a
table.
INPUT:
- ``inside`` - a string; HTML
OUTPUT:
- a string - HTML format
EXAMPLES::
sage: B = sagenb.notebook.interact.InputBox('x',2)
sage: sagenb.notebook.interact.InteractCanvas([B], 3).wrap_in_outside_frame('<!--inside-->')
'...notruncate...div...interact...table...inside...'
"""
return """<!--notruncate-->
<div padding=6 id="div-interact-%s">
<table width=800px height=20px bgcolor="#c5c5c5" cellpadding=15>
<tr>
<td bgcolor="#f9f9f9" valign=top align=left>%s</td>
</tr>
</table>
</div>""" % (self.cell_id(), inside)
# The following could be used to make the interact frame resizable
# and/or draggable. Neither effect is as cool as it sounds!
# <script>
# setTimeout(function() {
# $('#div-interact-%s').resizable();
# $('#div-interact-%s').draggable();
# }, 1);
# </script>
def render(self):
"""
Render in text (HTML) the entire :func:`interact` canvas.
OUTPUT:
- string - HTML format
EXAMPLES::
sage: B = sagenb.notebook.interact.InputBox('x',2)
sage: sagenb.notebook.interact.InteractCanvas([B], 3).render()
'...notruncate...div...interact...table...x...'
"""
html_controls={}
for side in ('top','left','right','bottom'):
html_controls[side] = self.render_controls(side=side)
s = """
<table>
<tr><td colspan=3>{top}</td></tr>
<tr><td>{left}</td><td style='width: 100%;'>{output}</td><td>{right}</td></tr>
<tr><td colspan=3>{bottom}</td></tr>
</table>""".format(output=self.render_output(), **html_controls)
s = self.wrap_in_outside_frame(s)
return s
class JavascriptCodeButton(InteractElement):
def __init__(self, label, code):
"""
This :func:`interact` element displays a button which when clicked
executes JavaScript code in the notebook.
EXAMPLES::
sage: b = sagenb.notebook.interact.JavascriptCodeButton('Push me', 'alert("2")')
"""
self.__label = label
self.__code = code
InteractElement.__init__(self)
def render(self):
r"""
Returns the HTML to display this button.
OUTPUT:
- a string - HTML format
EXAMPLES::
sage: b = sagenb.notebook.interact.JavascriptCodeButton('Push me', 'alert("2")')
sage: b.render()
'...input...button...Push me...alert("2")...'
"""
return """<input type="button" value="%s" onclick="%s">\n""" % (
self.__label, self.__code)
class UpdateButton(JavascriptCodeButton):
def __init__(self, cell_id, var):
r"""
Creates an :func:`interact` button element. A click on the
button triggers recomputation of the cell with the current
values of the variables.
INPUT:
- ``cell_id`` - an integer or string; the ambient cell's ID
- ``var``` - a variable which is used in the layout
EXAMPLES::
sage: b = sagenb.notebook.interact.UpdateButton(0, 'auto_update')
sage: b.render()
'...input...button...Update...0...'
"""
s = "interact(%r, {}, 1)" % cell_id
JavascriptCodeButton.__init__(self, "Update", s)
self.__var = var
def var(self):
"""
Return the name of the variable that this control interacts.
OUTPUT:
- a string - name of a variable as a string.
EXAMPLES::
sage: sagenb.notebook.interact.UpdateButton(1, 'auto_update').var()
'auto_update'
"""
return self.__var
from sage.misc.decorators import decorator_defaults
@decorator_defaults
def interact(f, layout=None, width='800px'):
r"""
Use interact as a decorator to create interactive Sage notebook
cells with sliders, text boxes, radio buttons, check boxes, and
color selectors. Simply put ``@interact`` on the line before a
function definition in a cell by itself, and choose appropriate
defaults for the variable names to determine the types of
controls (see tables below).
INPUT:
- ``f`` - a Python function
- ``layout`` (optional) - a dictionary with keys 'top', 'bottom', 'left', 'right' and values lists of rows of control variable names. Controls are laid out according to this pattern. If ``layout`` is not a dictionary, it is assumed to be the 'top' value. If ``layout`` is None, then all controls are assigned separate rows in the ``top`` value.
EXAMPLES:
In each example below we use a single underscore for the function
name. You can use *any* name you want; it does not have to
be an underscore.
We create an interact control with two inputs, a text input for
the variable ``a`` and a ``y`` slider that runs through the range of
integers from `0` to `19`.
::
sage: @interact
....: def _(a=5, y=(0..20)): print(a + y)
....:
<html>...
::
sage: @interact(layout=[['a','b'],['d']])
....: def _(a=x^2, b=(0..20), c=100, d=x+1): print(a+b+c+d)
....:
<html>...
::
sage: @interact(layout={'top': [['a', 'b']], 'left': [['c']], 'bottom': [['d']]})
....: def _(a=x^2, b=(0..20), c=100, d=x+1): print(a+b+c+d)
....:
<html>...
Draw a plot interacting with the "continuous" variable ``a``. By
default continuous variables have exactly 50 possibilities.
::
sage: @interact
....: def _(a=(0,2)):
....: show(plot(sin(x*(1+a*x)), (x,0,6)), figsize=4)
<html>...
Interact a variable in steps of 1 (we also use an unnamed
function)::
sage: @interact
....: def _(n=(10,100,1)):
....: show(factor(x^n - 1))
<html>...
Interact two variables::
sage: @interact
....: def _(a=(1,4), b=(0,10)):
....: show(plot(sin(a*x+b), (x,0,6)), figsize=3)
<html>...
Place a block of text among the controls::
sage: @interact
....: def _(t1=text_control("Factors an integer."), n="1"):
....: print(factor(Integer(n)))
<html>...
If your the time to evaluate your function takes awhile, you may
not want to have it reevaluated every time the inputs change. In
order to prevent this, you can add a keyword ``auto_update=False`` to
your function to prevent it from updating whenever the values are
changed. This will cause a button labeled 'Update' to appear
which you can click on to re-evaluate your function.
::
sage: @interact
....: def _(n=(10,100,1), auto_update=False):
....: show(factor(x^n - 1))
<html>...
DEFAULTS:
Defaults for the variables of the input function determine
interactive controls. The standard controls are ``input_box``,
``slider``, ``range_slider``, ``checkbox``, ``selector``,
``input_grid``, and ``color_selector``. There is also a text
control (see the defaults below).
* ``u = input_box(default=None, label=None, type=None)`` -
input box with given ``default``; use ``type=str`` to get
input as an arbitrary string
* ``u = slider(vmin, vmax=None, step_size=1, default=None,
label=None)`` - slider with given list of possible values;
``vmin`` can be a list
* ``u = range_slider(vmin, vmax=None, step_size=1,
default=None, label=None)`` - range slider with given list
of possible values; ``vmin`` can be a list
* ``u = checkbox(default=True, label=None)`` - a checkbox
* ``u = selector(values, label=None, nrows=None, ncols=None,
buttons=False)`` - a dropdown menu or buttons (get buttons
if ``nrows``, ``ncols``, or ``buttons`` is set, otherwise a
dropdown menu)
* ``u = input_grid(nrows, ncols, default=None, label=None,
to_value=lambda x:x, width=4)`` - an editable grid of
objects (a matrix or array)
* ``u = color_selector(default=(0,0,1), label=None,
widget='colorpicker', hide_box=False)`` - a color selector with a
possibly hidden input box; in Debian other values for ``widget``
are not supported.
* ``u = text_control(value='')`` - a block of text
You can also create a color selector by setting the default value for
an ``input_box`` to ``Color(...)``.
There are also some convenient defaults that allow you to make
controls automatically without having to explicitly specify them.
E.g., you can make ``x`` a continuous slider of values between ``u``
and ``v`` by just writing ``x=(u,v)`` in the argument list of
your function. These are all just convenient shortcuts for
creating the controls listed above.
* ``u`` - blank input_box field
* ``u = element`` - input_box with ``default=element``, if
element not below.
* ``u = (umin,umax)`` - continuous slider (really `100` steps)
* ``u = (umin,umax,du)`` - slider with step size ``du``
* ``u = list`` - buttons if ``len(list)`` at most `5`;
otherwise, drop down
* ``u = iterator`` (e.g. a generator) - a slider (up to `10000`
steps)
* ``u = bool`` - a checkbox
* ``u = Color('blue')`` - a color selector; returns ``Color``
object
* ``u = (default, v)`` - ``v`` as above, with given
``default`` value
* ``u = (label, v)`` - ``v`` as above, with given ``label``
(a string)
* ``u = matrix`` - an ``input_grid`` with ``to_value`` set to
``matrix.parent()`` and default values given by the matrix
.. note::
Suppose you would like to make an interactive with a default
RGB color of ``(1,0,0)``, so the function would have signature
``f(color=(1,0,0))``. Unfortunately, the above shortcuts
reinterpret the ``(1,0,0)`` as a discrete slider with step
size 0 between 1 and 0. Instead you should do the
following::
sage: @interact
....: def _(v = input_box((1,0,0))):
....: show(plot(sin,color=v))
<html>...
An alternative::
sage: @interact
....: def _(c = color_selector((1, 0, 0))):
....: show(plot(sin, color = c))
<html>...
MORE EXAMPLES:
We give an input box that allows one to enter completely arbitrary
strings::
sage: @interact
....: def _(a=input_box('sage', label="Enter your name", type=str)):
....: print("Hello there %s"%a.capitalize())
<html>...
The scope of variables that you control via :func:`interact` are local
to the scope of the function being interacted with. However, by
using the ``global`` Python keyword, you can still modify global
variables as follows::
sage: xyz = 10
sage: @interact
....: def _(a=('xyz',5)):
....: global xyz
....: xyz = a
<html>...
If you enter the above you obtain an :func:`interact` canvas.
Entering values in the box changes the global variable ``xyz``.
Here's a example with several controls::
sage: @interact
....: def _(title=["A Plot Demo", "Something silly", "something tricky"], a=input_box(sin(x*sin(x*sin(x))), 'function'),
....: clr = Color('red'), thickness=[1..30], zoom=(1,0.95,..,0.1), plot_points=(200..2000)):
....: html('<h1 align=center>%s</h1>'%title)
....: print(plot_points)
....: show(plot(a, -zoom*pi,zoom*pi, color=clr, thickness=thickness, plot_points=plot_points))
<html>...
For a more compact color control, use an empty label and
hide the input box::
sage: @interact
....: def _(color=color_selector((1,0,1), label='', hide_box=True)):
....: show(plot(x/(8/7+sin(x)), (x,-50,50), fill=True, fillcolor=color))
<html>...
We give defaults and name the variables::
sage: @interact
....: def _(a=('first', (1,4)), b=(0,10)):
....: show(plot(sin(a*x+sin(b*x)), (x,0,6)), figsize=3)
<html>...
Another example involving labels, defaults, and the slider
command::
sage: @interact
....: def _(a = slider(1, 4, default=2, label='Multiplier'),
....: b = slider(0, 10, default=0, label='Phase Variable')):
....: show(plot(sin(a*x+b), (x,0,6)), figsize=4)
<html>...
An example where the range slider control is useful::
sage: @interact
....: def _(b = range_slider(-20, 20, 1, default=(-19,3), label='Range')):
....: plot(sin(x)/x, b[0], b[1]).show(xmin=b[0],xmax=b[1])
<html>...
An example using checkboxes, obtained by making the default values
bools::
sage: @interact
....: def _(axes=('Show axes', True), square=False):
....: show(plot(sin, -5,5), axes=axes, aspect_ratio = (1 if square else None))
<html>...
An example generating a random walk that uses a checkbox control
to determine whether points are placed at each step::
sage: @interact
....: def foo(pts = checkbox(True, "points"), n = (50,(10..100))):
....: s = 0; v = [(0,0)]
....: for i in range(n):
....: s += random() - 0.5
....: v.append((i, s))
....: L = line(v, rgbcolor='#4a8de2')
....: if pts: L += points(v, pointsize=20, rgbcolor='black')
....: show(L)
<html>...
You can rotate and zoom into 3-D graphics while interacting with a
variable::
sage: @interact
....: def _(a=(0,1)):
....: x,y = var('x,y')
....: show(plot3d(sin(x*cos(y*a)), (x,0,5), (y,0,5)), figsize=4)
<html>...
A random polygon::
sage: pts = [(random(), random()) for _ in xrange(20)]
sage: @interact
....: def _(n = (4..len(pts)), c=Color('purple') ):
....: G = points(pts[:n],pointsize=60) + polygon(pts[:n], rgbcolor=c)
....: show(G, figsize=5, xmin=0, ymin=0)
<html>...
Two "sinks" displayed simultaneously via a contour plot and a 3-D
interactive plot::
sage: @interact
....: def _(q1=(-1,(-3,3)), q2=(-2,(-3,3))):
....: x,y = var('x,y')
....: f = q1/sqrt((x+1)^2 + y^2) + q2/sqrt((x-1)^2+(y+0.5)^2)
....: C = contour_plot(f, (-2,2), (-2,2), plot_points=30, contours=15, cmap='cool')
....: show(C, figsize=3, aspect_ratio=1)
....: show(plot3d(f, (x,-2,2), (y,-2,2)), figsize=4)
<html>...
This is similar to above, but you can select the color map from a
dropdown menu::
sage: @interact
....: def _(q1=(-1,(-3,3)), q2=(-2,(-3,3)),
....: cmap=['autumn', 'bone', 'cool', 'copper', 'gray', 'hot', 'hsv',
....: 'jet', 'pink', 'prism', 'spring', 'summer', 'winter']):
....: x,y = var('x,y')
....: f = q1/sqrt((x+1)^2 + y^2) + q2/sqrt((x-1)^2+(y+0.5)^2)
....: C = contour_plot(f, (x,-2,2), (y,-2,2), plot_points=30, contours=15, cmap=cmap)
....: show(C, figsize=3, aspect_ratio=1)
<html>...
A quadratic roots etch-a-sketch::
sage: v = []
sage: html('<h2>Quadratic Root Etch-a-sketch</h2>')
<html>...<h2>Quadratic Root Etch-a-sketch</h2>...</html>
sage: @interact
....: def _(a=[-10..10], b=[-10..10], c=[-10..10]):
....: f = a*x^2 + b*x + c == 0; show(f)
....: soln = solve(a*x^2 + b*x + c == 0, x)[0].rhs()
....: show(soln)
....: P = tuple(CDF(soln))
....: v.append(P)
....: show(line(v, rgbcolor='purple') + point(P, pointsize=200))
<html>...
In the following example, we only generate data for a given ``n``
once, so that as one varies ``p`` the data does not randomly change.
We do this by simply caching the results for each ``n`` in a
dictionary.::
sage: data = {}
sage: @interact
....: def _(n=(500,(100,5000,1)), p=(1,(0.1,10))):
....: n = int(n)
....: if n not in data:
....: data[n] = [(random(), random()) for _ in xrange(n)]
....: show(points([(x^p,y^p) for x,y in data[n]], rgbcolor='black'), xmin=0, ymin=0, axes=False)
<html>...
A conchoid::
sage: @interact
....: def _(k=(1.2,(1.1,2)), k_2=(1.2,(1.1,2)), a=(1.5,(1.1,2))):
....: u, v = var('u,v')
....: f = (k^u*(1+cos(v))*cos(u), k^u*(1+cos(v))*sin(u), k^u*sin(v)-a*k_2^u)
....: show(parametric_plot3d(f, (u,0,6*pi), (v,0,2*pi), plot_points=[40,40], texture=(0,0.5,0)))
<html>...
An input grid::
sage: @interact
....: def _(A=matrix(QQ,3,3,range(9)), v=matrix(QQ,3,1,range(3))):
....: try:
....: x = A\v
....: html('$$%s %s = %s$$'%(latex(A), latex(x), latex(v)))
....: except:
....: html('There is no solution to $$%s x=%s$$'%(latex(A), latex(v)))
<html>...
"""
if not isinstance(f, types.FunctionType) and callable(f):
f = f.__call__
(args, varargs, varkw, defaults) = inspect.getargspec(f)
if defaults is None:
defaults = []
n = len(args) - len(defaults)
controls = [automatic_control(defaults[i - n] if i >= n else None).render(arg)
for i, arg in enumerate(args)]
variables = {}
adapt = {}
state[SAGE_CELL_ID] = {'variables': variables, 'adapt': adapt}
for control in controls:
variables[control.var()] = control.default_value()
adapt[control.adapt_number()] = control._adaptor
# Replace the auto_update checkbox with a button that will cause
# the cell to recompute itself.
# TODO: Should auto_update=True yield a checkbox?
auto_update = variables.get('auto_update', True)
if auto_update is False:
i = args.index('auto_update')
controls[i] = UpdateButton(SAGE_CELL_ID, 'auto_update')
C = InteractCanvas(controls, SAGE_CELL_ID, auto_update=auto_update, layout=layout,
width=width)
html(C.render())
def _():
z = f(*[variables[arg] for arg in args])
if z:
print(z)
state[SAGE_CELL_ID]['function'] = _
return f
######################################################
# Actual control objects that the user passes in
######################################################
class control:
def __init__(self, label=None):
"""
An interactive control object used with the :func:`interact` command.
This is the abstract base class.
INPUTS:
- ``label`` - a string
EXAMPLES::
sage: sagenb.notebook.interact.control('a control')
Interative control 'a control' (abstract base class)
"""
self.__label = label
def __repr__(self):
"""
Return string representation of this control. (It just
mentions the label and that this is an abstract base class.)
OUTPUT:
- a string
EXAMPLES::
sage: sagenb.notebook.interact.control('a control').__repr__()
"Interative control 'a control' (abstract base class)"
"""
return "Interative control '%s' (abstract base class)" % self.__label
def label(self):
"""
Return the label of this control.
OUTPUT:
- a string
EXAMPLES::
sage: sagenb.notebook.interact.control('a control').label()
'a control'
sage: selector([1,2,7], 'alpha').label()
'alpha'
"""
return self.__label
def set_label(self, label):
"""
Set the label of this control.
INPUT:
- ``label`` - a string
EXAMPLES::
sage: C = sagenb.notebook.interact.control('a control')
sage: C.set_label('sage'); C
Interative control 'sage' (abstract base class)
"""
self.__label = label
class input_box(control):
def __init__(self, default=None, label=None, type=None, width=80, height=1, **kwargs):
r"""
An input box interactive control. Use this in conjunction
with the :func:`interact` command.
INPUT:
- ``default`` - an object; the default put in this input box
- ``label`` - a string; the label rendered to the left of the
box.
- ``type`` - a type; coerce inputs to this; this doesn't
have to be an actual type, since anything callable will do.
- ``height`` - an integer (default: 1); the number of rows.
If greater than 1 a value won't be returned until something
outside the textarea is clicked.
- ``width`` - an integer; width of text box in characters
- ``kwargs`` - a dictionary; additional keyword options
EXAMPLES::
sage: input_box("2+2", 'expression')
Interact input box labeled 'expression' with default value '2+2'
sage: input_box('sage', label="Enter your name", type=str)
Interact input box labeled 'Enter your name' with default value 'sage'
sage: input_box('Multiline\nInput',label='Click to change value',type=str,height=5)
Interact input box labeled 'Click to change value' with default value 'Multiline\nInput'
"""
self.__default = default
self.__type = type
control.__init__(self, label)
self.__width = width
self.__height = height
self.__kwargs = kwargs
def __repr__(self):
"""
Return print representation of this input box.
OUTPUT:
- a string
EXAMPLES::
sage: input_box("2+2", 'expression').__repr__()
"Interact input box labeled 'expression' with default value '2+2'"
"""
return "Interact input box labeled %r with default value %r"%(
self.label(), self.__default)
def default(self):
"""
Return the default value of this input box.
OUTPUT:
- an object
EXAMPLES::
sage: input_box('2+2', 'Expression').default()
'2+2'
sage: input_box(x^2 + 1, 'Expression').default()
x^2 + 1
sage: checkbox(True, "Points").default()
True
"""
return self.__default
def type(self):
"""
Return the type that elements of this input box are coerced to
or None if they are not coerced (they have whatever type
they evaluate to).
OUTPUT:
- a type
EXAMPLES::
sage: input_box("2+2", 'expression', type=int).type()
<type 'int'>
sage: input_box("2+2", 'expression').type() is None
True
"""
return self.__type
def render(self, var):
r"""
Return rendering of this input box as an :class:`InputBox` to be
used for an :func:`interact` canvas. Basically this specializes
this input to be used for a specific function and variable.
INPUT:
- ``var`` - a string (variable; one of the variable names
input to ``f``)
OUTPUT:
- an :class:`InputBox` instance
EXAMPLES::
sage: input_box("2+2", 'Exp').render('x')
An InputBox interactive control with x='2+2' and label 'Exp'
"""
if self.__type is Color:
return ColorInput(var, default_value=self.__default,
label=self.label(), type=self.__type,
**self.__kwargs)
else:
return InputBox(var, default_value=self.__default,
height=self.__height,
label=self.label(), type=self.__type,
width=self.__width, **self.__kwargs)
class color_selector(input_box):
def __init__(self, default=(0, 0, 1), label=None,
widget='colorpicker', hide_box=False):
r"""
A color selector (also called a color chooser, picker, or
tool) interactive control. Use this with the :func:`interact`
command.
INPUT:
- ``default`` - an instance of or valid constructor argument
to :class:`Color` (default: (0,0,1)); the selector's default
color; a string argument must be a valid color name (e.g.,
'red') or HTML hex color (e.g., '#abcdef')
- ``label`` - a string (default: None); the label rendered to
the left of the selector.
- ``widget`` - a string (default: 'colorpicker'); the color
selector widget to use; choices are 'colorpicker'; in Debian
other values are not supported.
- ``hide_box`` - a boolean (default: False); whether to hide
the input box associated with the color selector widget
EXAMPLES::
sage: color_selector()
Interact color selector labeled None, with default RGB color (0.0, 0.0, 1.0), widget 'colorpicker', and visible input box
sage: color_selector(default = Color(0, 0.5, 0.25))
Interact color selector labeled None, with default RGB color (0.0, 0.5, 0.25), widget 'colorpicker', and visible input box
sage: color_selector('purple', widget = 'colorpicker')
Interact color selector labeled None, with default RGB color (0.50..., 0.0, 0.50...), widget 'colorpicker', and visible input box
sage: color_selector('crayon', widget = 'colorpicker')
Traceback (most recent call last):
...
ValueError: unknown color 'crayon'
"""
input_box.__init__(self, default=Color(default), label=label,
type=Color, widget=widget, hide_box=hide_box)
self.__widget = widget
self.__hide_box = hide_box
def __repr__(self):
"""
Return the print representation of this color selector.
OUTPUT:
- a string
EXAMPLES::
sage: color_selector(Color('red'), 'line color').__repr__()
"Interact color selector labeled 'line color', with default RGB color (1.0, 0.0, 0.0), widget 'colorpicker', and visible input box"
"""
s = "Interact color selector labeled %r, with default %r, widget %r, and " % (self.label(), self.default(), self.widget())
if self.hide_box():
return s + "hidden input box"
else:
return s + "visible input box"
def widget(self):
"""
Return the name of the HTML widget for this color selector.
OUTPUT:
- a string; the widget's name
EXAMPLES::
sage: color_selector().widget()
'colorpicker'
sage: color_selector(default=Color(0,0.5,0.25)).widget()
'colorpicker'
"""
return self.__widget
def hide_box(self):
"""
Return whether to hide the input box associated with this color
selector.
OUTPUT:
- a boolean
EXAMPLES::
sage: color_selector().hide_box()
False
sage: color_selector((0.75,0.5,0.25)).hide_box()
False
"""
return self.__hide_box
class input_grid(control):
def __init__(self, nrows, ncols, default=None, label=None,
to_value=lambda x: x, width=4):
r"""
An input grid interactive control. Use this in conjunction
with the :func:`interact` command.
INPUT:
- ``nrows`` - an integer
- ``ncols`` - an integer
- ``default`` - an object; the default put in this input box
- ``label`` - a string; the label rendered to the left of the
box.
- ``to_value`` - a list; the grid output (list of rows) is
sent through this function. This may reformat the data or
coerce the type.
- ``width`` - an integer; size of each input box in characters
NOTEBOOK EXAMPLE::
@interact
def _(m = input_grid(2,2, default = [[1,7],[3,4]],
label='M=', to_value=matrix),
v = input_grid(2,1, default=[1,2],
label='v=', to_value=matrix)):
try:
x = m\v
html('$$%s %s = %s$$'%(latex(m), latex(x), latex(v)))
except:
html('There is no solution to $$%s x=%s$$'%(latex(m), latex(v)))
EXAMPLES::
sage: input_grid(2,2, default = 0, label='M')
Interact 2 x 2 input grid control labeled M with default value 0
sage: input_grid(2,2, default = [[1,2],[3,4]], label='M')
Interact 2 x 2 input grid control labeled M with default value [[1, 2], [3, 4]]
sage: input_grid(2,2, default = [[1,2],[3,4]], label='M', to_value=MatrixSpace(ZZ,2,2))
Interact 2 x 2 input grid control labeled M with default value [[1, 2], [3, 4]]
sage: input_grid(1, 3, default=[[1,2,3]], to_value=lambda x: vector(flatten(x)))
Interact 1 x 3 input grid control labeled None with default value [[1, 2, 3]]
"""
self.__default = default
self.__rows = nrows
self.__columns = ncols
self.__to_value = to_value
self.__width = width
control.__init__(self, label)
def __repr__(self):
"""
Return print representation of this input box.
OUTPUT:
- a string
EXAMPLES::
sage: input_grid(2,2, label='M').__repr__()
'Interact 2 x 2 input grid control labeled M with default value None'
"""
return 'Interact %r x %r input grid control labeled %s with default value %s'%(
self.__rows, self.__columns, self.label(), self.default())
def default(self):
"""
Return the default value of this input grid.
OUTPUT:
- an object
EXAMPLES::
sage: input_grid(2,2, default=1).default()
1
"""
return self.__default
def render(self, var):
r"""
Return rendering of this input grid as an :class:`InputGrid` to be
used for an :func:`interact` canvas. Basically this specializes
this input to be used for a specific function and variable.
INPUT:
- ``var`` - a string (variable; one of the variable names
input to ``f``)
OUTPUT:
- an :class:`InputGrid` instance.
EXAMPLES::
sage: input_grid(2,2).render('x')
A 2 x 2 InputGrid interactive control with x=[[None, None], [None, None]] and label 'x'
"""
return InputGrid(var, rows=self.__rows, columns=self.__columns,
default_value=self.__default, label=self.label(),
to_value=self.__to_value, width=self.__width)
class checkbox(input_box):
def __init__(self, default=True, label=None):
"""
A checkbox interactive control. Use this in conjunction with
the :func:`interact` command.
INPUT:
- ``default`` - a bool (default: True); whether box should be
checked or not
- ``label`` - a string (default: None) text label rendered to
the left of the box
EXAMPLES::
sage: checkbox(False, "Points")
Interact checkbox labeled 'Points' with default value False
sage: checkbox(True, "Points")
Interact checkbox labeled 'Points' with default value True
sage: checkbox(True)
Interact checkbox labeled None with default value True
sage: checkbox()
Interact checkbox labeled None with default value True
"""
input_box.__init__(self, bool(default), label=label, type=bool)
def __repr__(self):
"""
Print representation of this checkbox.
OUTPUT:
- a string
EXAMPLES::
sage: checkbox(True, "Points").__repr__()
"Interact checkbox labeled 'Points' with default value True"
"""
return "Interact checkbox labeled %r with default value %r"% (
self.label(), self.default())
class slider_generic(control):
def __init__(self, vmin, vmax=None, step_size=None, label=None,
display_value=True):
control.__init__(self, label=label)
self.__display_value = display_value
self.__vmin = vmin
self.__vmax = vmax
self.__step_size = step_size
@cached_method
def values(self):
"""
Returns list of values that this slider takes on, in order.
OUTPUT:
- a list
.. note:: This is a reference to a mutable list.
EXAMPLES::
sage: sagenb.notebook.interact.slider(1,10,1/2).values()
[1, 3/2, 2, 5/2, 3, 7/2, 4, 9/2, 5, 11/2, 6, 13/2, 7, 15/2, 8, 17/2, 9, 19/2, 10]
"""
if isinstance(self.__vmin, list):
vals = self.__vmin
else:
if self.__vmax is None:
self.__vmax = self.__vmin
self.__vmin = 0
# Compute step size; vmin and vmax are both defined here
# 500 is the length of the slider (in px)
if self.__step_size is None:
self.__step_size = (self.__vmax-self.__vmin) / 499.0
elif self.__step_size <= 0:
raise ValueError("invalid negative step size -- step size must be positive")
#Compute list of values
num_steps = int(math.ceil((self.__vmax-self.__vmin)/float(self.__step_size)))
if num_steps <= 1:
vals = [self.__vmin, self.__vmax]
else:
vals = srange(self.__vmin, self.__vmax, self.__step_size, include_endpoint=True)
if vals[-1] != self.__vmax:
try:
if vals[-1] > self.__vmax:
vals[-1] = self.__vmax
else:
vals.append(self.__vmax)
except (ValueError, TypeError):
pass
# Is the list of values is small (len < =50), use the whole
# list. Otherwise, use part of the list.
if len(vals) == 0:
return [0]
elif(len(vals) <= 500):
return vals
else:
vlen = (len(vals)-1) / 499.0
return [vals[(int)(i*vlen)] for i in range(500)]
def display_value(self):
"""
Returns whether to display the value on the slider.
OUTPUT:
- a bool
EXAMPLES::
sagenb.notebook.interact.slider_generic(1,10,1/2).display_value()
True
"""
return self.__display_value
class slider(slider_generic):
def __init__(self, vmin, vmax=None, step_size=None, default=None,
label=None, display_value=True):
r"""
An interactive slider control, which can be used in conjunction
with the :func:`interact` command.
INPUT:
- ``vmin`` - an object
- ``vmax`` - an object (default: None); if None then ``vmin``
must be a list, and the slider then varies over elements of
the list.
- ``step_size`` - an integer (default: 1)
- ``default`` - an object (default: None); default value is
"closest" in ``vmin`` or range to this default.
- ``label`` - a string
- ``display_value`` - a bool, whether to display the current
value to the right of the slider
EXAMPLES:
We specify both ``vmin`` and ``vmax``. We make the default
`3`, but since `3` isn't one of `3/17`-th spaced values
between `2` and `5`, `52/17` is instead chosen as the
default (it is closest)::
sage: slider(2, 5, 3/17, 3, 'alpha')
Slider: alpha [2--|52/17|---5]
Here we give a list::
sage: slider([1..10], None, None, 3, 'alpha')
Slider: alpha [1--|3|---10]
The elements of the list can be anything::
sage: slider([1, 'x', 'abc', 2/3], None, None, 'x', 'alpha')
Slider: alpha [1--|x|---2/3]
"""
slider_generic.__init__(self, vmin, vmax, step_size, label,
display_value)
self.__default = default
def __repr__(self):
"""
Return string representation of this slider.
OUTPUT:
- a string
EXAMPLES::
sage: slider(2, 5, 1/5, 3, 'alpha').__repr__()
'Slider: alpha [2--|3|---5]'
"""
return "Slider: %s [%s--|%s|---%s]" % (
self.label(), self.values()[0],
self.values()[self.default_index()], self.values()[-1])
@cached_method
def default_index(self):
"""
Return default index into the list of values.
OUTPUT:
- an integer
EXAMPLES::
sage: slider(2, 5, 1/2, 3, 'alpha').default_index()
2
"""
# determine the best choice of index into the list of values
# for the user-selected default
if self.__default is None:
return 0
else:
try:
i = self.values().index(self.__default)
except ValueError:
# here no index matches - which is best?
try:
v = [(abs(self.__default - self.values()[j]), j) for j in range(len(self.values()))]
m = min(v)
i = m[1]
except TypeError: # abs not defined on everything, so give up
i = 0
return i
def render(self, var):
"""
Render the :func:`interact` control for the given function and
variable.
INPUT:
- ``var`` - a string; variable name
OUTPUT:
- a :class:`Slider` instance
EXAMPLES::
sage: S = slider(0, 10, 1, default=3, label='theta'); S
Slider: theta [0--|3|---10]
sage: S.render('x')
Slider Interact Control: theta [0--|3|---10]
sage: slider(2, 5, 2/7, 3, 'alpha').render('x')
Slider Interact Control: alpha [2--|20/7|---5]
"""
return Slider(var, self.values(), self.default_index(),
label=self.label(), display_value=self.display_value())
class range_slider(slider_generic):
def __init__(self, vmin, vmax=None, step_size=None, default=None, label=None,
display_value=True):
r"""
An interactive range slider control, which can be used in conjunction
with the :func:`interact` command.
INPUT:
- ``vmin`` - an object
- ``vmax`` - object or None; if None then ``vmin`` must be a
list, and the slider then varies over elements of the list.
- ``step_size`` - integer (default: 1)
- ``default`` - a 2-tuple of objects (default: None); default
range is "closest" in ``vmin`` or range to this default.
- ``label`` - a string
- ``display_value`` - a bool, whether to display the current
value below the slider
EXAMPLES:
We specify both ``vmin`` and ``vmax``. We make the default
`(3,4)` but since neither is one of `3/17`-th spaced
values between `2` and `5`, the closest values: `52/17`
and `67/17`, are instead chosen as the default::
sage: range_slider(2, 5, 3/17, (3,4), 'alpha')
Range Slider: alpha [2--|52/17==67/17|---5]
Here we give a list::
sage: range_slider([1..10], None, None, (3,7), 'alpha')
Range Slider: alpha [1--|3==7|---10]
"""
slider_generic.__init__(self, vmin, vmax, step_size, label,
display_value)
self.__default = default
def __repr__(self):
"""
Return string representation of this slider.
OUTPUT:
- a string
EXAMPLES::
sage: range_slider(2, 5, 1/5, (3,4), 'alpha').__repr__()
'Range Slider: alpha [2--|3==4|---5]'
"""
return "Range Slider: %s [%s--|%s==%s|---%s]" % (
self.label(), self.values()[0],
self.values()[self.default_index()[0]],
self.values()[self.default_index()[1]], self.values()[-1])
@cached_method
def default_index(self):
"""
Return default index into the list of values.
OUTPUT:
- an integer 2-tuple
EXAMPLES::
sage: range_slider(2, 5, 1/2, (3,4), 'alpha').default_index()
(2, 4)
"""
# Determine the best choice of index into the list of values
# for the user-selected default.
if self.__default is None:
return (0,1)
elif not isinstance(self.__default, tuple) or len(self.__default) != 2:
raise TypeError("default value must be None or a 2-tuple.")
else:
dlist = []
for i in [0, 1]:
try:
d = self.values().index(self.__default[i])
except ValueError:
# Here no index matches -- which is best?
try:
v = [(abs(self.__default[i] - self.values()[j]), j) for j in range(len(self.values()))]
m = min(v)
d = m[1]
except TypeError:
# abs not defined on everything, so give up
d = 0
dlist.append(d)
return (dlist[0], dlist[1])
def render(self, var):
"""
Render the :func:`interact` control for the given function and
variable.
INPUT:
- ``var`` - string; variable name
OUTPUT:
- a :class:`RangeSlider` instance
EXAMPLES::
sage: S = range_slider(0, 10, 1, default=(3,7), label='theta'); S
Range Slider: theta [0--|3==7|---10]
sage: S.render('x')
Range Slider Interact Control: theta [0--|3==7|---10]
sage: range_slider(2, 5, 2/7, (3,4), 'alpha').render('x')
Range Slider Interact Control: alpha [2--|20/7==4|---5]
"""
return RangeSlider(var, self.values(), self.default_index(),
label=self.label(),
display_value=self.display_value())
class selector(control):
def __init__(self, values, label=None, default=None,
nrows=None, ncols=None, width=None, buttons=False):
r"""
A drop down menu or a button bar that when pressed sets a
variable to a given value. Use this in conjunction with the
:func:`interact` command.
We use the same command to create either a drop down menu or
selector bar of buttons, since conceptually the two controls
do exactly the same thing - they only look different. If
either ``nrows`` or ``ncols`` is given, then you get a buttons
instead of a drop down menu.
INPUT:
- ``values`` - [val0, val1, val2, ...] or [(val0, lbl0),
(val1,lbl1), ...] where all labels must be given or given as
None.
- ``label`` - a string (default: None); if given, this label
is placed to the left of the entire button group
- ``default`` - an object (default: 0); default value in values
list
- ``nrows`` - an integer (default: None); if given determines
the number of rows of buttons; if given buttons option below
is set to True
- ``ncols`` - an integer (default: None); if given determines
the number of columns of buttons; if given buttons option
below is set to True
- ``width`` - an integer (default: None); if given, all
buttons are the same width, equal to this in HTML ex
units's.
- ``buttons`` - a bool (default: False); if True, use buttons
EXAMPLES::
sage: selector([1..5])
Drop down menu with 5 options
sage: selector([1,2,7], default=2)
Drop down menu with 3 options
sage: selector([1,2,7], nrows=2)
Button bar with 3 buttons
sage: selector([1,2,7], ncols=2)
Button bar with 3 buttons
sage: selector([1,2,7], width=10)
Drop down menu with 3 options
sage: selector([1,2,7], buttons=True)
Button bar with 3 buttons
We create an :func:`interact` that involves computing charpolys of
matrices over various rings::
sage: @interact
....: def _(R=selector([ZZ,QQ,GF(17),RDF,RR]), n=(1..10)):
....: M = random_matrix(R, n)
....: show(M)
....: show(matrix_plot(M,cmap='Oranges'))
....: f = M.charpoly()
....: print(f)
<html>...
Here we create a drop-down::
sage: @interact
....: def _(a=selector([(2,'second'), (3,'third')])):
....: print(a)
<html>...
"""
if nrows is not None or ncols is not None:
buttons = True
if default is None:
default = 0
else:
try:
default = values.index(default)
except IndexError:
default = 0
self.__values = values
self.__nrows = nrows
self.__ncols = ncols
self.__width = width
self.__default = default
self.__buttons = buttons
control.__init__(self, label)
def __repr__(self):
"""
Return print representation of this button.
OUTPUT:
- a string
EXAMPLES::
sage: selector([1,2,7], default=2).__repr__()
'Drop down menu with 3 options'
"""
if self.__buttons:
return "Button bar with %s buttons" % len(self.__values)
else:
return "Drop down menu with %s options" % len(self.__values)
def values(self):
"""
Return the list of values or (val, lbl) pairs that this
selector can take on.
OUTPUT:
- a list
EXAMPLES::
sage: selector([1..5]).values()
[1, 2, 3, 4, 5]
sage: selector([(5,'fifth'), (8,'eight')]).values()
[(5, 'fifth'), (8, 'eight')]
"""
return self.__values
def default(self):
"""
Return the default choice for this control.
OUTPUT:
- an integer, with 0 corresponding to the first choice.
EXAMPLES::
sage: selector([1,2,7], default=2).default()
1
"""
return self.__default
def render(self, var):
r"""
Return rendering of this button as a :class:`Selector`
instance to be used for an :func:`interact` canvas.
INPUT:
- ``var`` - a string (variable; one of the variable names
input to ``f``)
OUTPUT:
- a :class:`Selector` instance
EXAMPLES::
sage: selector([1..5]).render('alpha')
Selector with 5 options for variable 'alpha'
"""
return Selector(var, values=self.__values, label=self.label(),
default=self.__default, nrows=self.__nrows,
ncols=self.__ncols, width=self.__width,
buttons=self.__buttons)
class text_control(control):
def __init__(self, value=''):
"""
Text that can be inserted among other :func:`interact` controls.
INPUT:
- ``value`` - HTML for the control
EXAMPLES::
sage: text_control('something')
Text field: something
"""
self.__default = value
control.__init__(self, '')
def __repr__(self):
"""
Return print representation of this control.
OUTPUT:
- a string
EXAMPLES::
sage: text_control('something')
Text field: something
"""
return "Text field: %s" % self.__default
def render(self, var):
"""
Return rendering of the text field
INPUT:
- ``var`` - a string (variable; one of the variable names
input to ``f``)
OUTPUT:
- a :class:`TextControl` instance
"""
return TextControl(var, self.__default)
def automatic_control(default):
"""
Automagically determine the type of control from the default
value of the variable.
INPUT:
- ``default`` - the default value for ``v`` given by the function;
see the documentation to :func:`interact` for details.
OUTPUT:
- an :func:`interact` control
EXAMPLES::
sage: sagenb.notebook.interact.automatic_control('')
Interact input box labeled None with default value ''
sage: sagenb.notebook.interact.automatic_control(15)
Interact input box labeled None with default value 15
sage: sagenb.notebook.interact.automatic_control(('start', 15))
Interact input box labeled 'start' with default value 15
sage: sagenb.notebook.interact.automatic_control((1,250))
Slider: None [1.0--|1.0|---250.0]
sage: sagenb.notebook.interact.automatic_control(('alpha', (1,250)))
Slider: alpha [1.0--|1.0|---250.0]
sage: sagenb.notebook.interact.automatic_control((2,(0,250)))
Slider: None [0.0--|2.00400801603|---250.0]
sage: sagenb.notebook.interact.automatic_control(('alpha label', (2,(0,250))))
Slider: alpha label [0.0--|2.00400801603|---250.0]
sage: sagenb.notebook.interact.automatic_control((2, ('alpha label',(0,250))))
Slider: alpha label [0.0--|2.00400801603|---250.0]
sage: C = sagenb.notebook.interact.automatic_control((1,52, 5)); C
Slider: None [1--|1|---52]
sage: C.values()
[1, 6, 11, 16, 21, 26, 31, 36, 41, 46, 51, 52]
sage: sagenb.notebook.interact.automatic_control((17, (1,100,5)))
Slider: None [1--|16|---100]
sage: sagenb.notebook.interact.automatic_control([1..4])
Button bar with 4 buttons
sage: sagenb.notebook.interact.automatic_control([1..100])
Drop down menu with 100 options
sage: sagenb.notebook.interact.automatic_control((1..100))
Slider: None [1--|1|---100]
sage: sagenb.notebook.interact.automatic_control((5, (1..100)))
Slider: None [1--|5|---100]
sage: sagenb.notebook.interact.automatic_control(matrix(2,2))
Interact 2 x 2 input grid control labeled None with default value [0, 0, 0, 0]
"""
label = None
default_value = None
for _ in range(2):
if isinstance(default, tuple) and len(default) == 2 and isinstance(default[0], str):
label, default = default
if isinstance(default, tuple) and len(default) == 2 and isinstance(default[1], (tuple, list, collections.Iterator)):
default_value, default = default
if isinstance(default, control):
C = default
if label:
C.set_label(label)
elif isinstance(default, str):
C = input_box(default, label=label, type=str)
elif isinstance(default, bool):
C = input_box(default, label=label, type=bool)
elif isinstance(default, list):
C = selector(default, default=default_value, label=label, buttons=len(default) <= 5)
elif isinstance(default, collections.Iterator):
C = slider(list_of_first_n(default, 10000), default=default_value, label=label)
elif isinstance(default, Color):
C = input_box(default, label=label, type=Color)
elif isinstance(default, tuple):
if len(default) == 2:
C = slider(default[0], default[1], default=default_value, label=label)
elif len(default) == 3:
C = slider(default[0], default[1], default[2], default=default_value, label=label)
else:
C = slider(list(default), default=default_value, label=label)
elif is_Matrix(default):
C = input_grid(default.nrows(), default.ncols(), default=default.list(), to_value=default.parent(), label=label)
else:
C = input_box(default, label=label)
return C
def list_of_first_n(v, n):
"""
Given an iterator v, return first n elements it produces as a list.
INPUT:
- ``v`` - an iterator
- ``n`` - an integer
OUTPUT:
- a list
EXAMPLES::
sage: from itertools import takewhile
sage: p100 = takewhile(lambda x: x < 100, Primes())
sage: sagenb.notebook.interact.list_of_first_n(p100, 10)
[2, 3, 5, 7, 11, 13, 17, 19, 23, 29]
sage: sagenb.notebook.interact.list_of_first_n((1..5), 10)
[1, 2, 3, 4, 5]
sage: sagenb.notebook.interact.list_of_first_n(QQ, 10)
[0, 1, -1, 1/2, -1/2, 2, -2, 1/3, -1/3, 3]
"""
if not hasattr(v, 'next'):
v = v.__iter__()
w = []
while n > 0:
try:
w.append(v.next())
except StopIteration:
return w
n -= 1
return w
def update(cell_id, var, adapt, value, globs):
r"""
Called when updating the positions of an interactive control.
Note that this just causes the values of the variables to be
updated; it does not reevaluate the function with the new values.
INPUT:
- ``cell_id`` - an integer or string; the ID of an
:func:`interact` cell
- ``var`` - an object; a variable associated to that cell
- ``adapt`` - in integer; the number of the adapt function
- ``value`` - an object; new value of the control
- ``globs`` - global variables.
EXAMPLES:
The following outputs __SAGE_INTERACT_RESTART__ to indicate that
not all the state of the :func:`interact` canvas has been set up yet
(this setup happens when JavaScript calls certain functions)::
sage: sagenb.notebook.interact.update(0, 'a', 0, '5', globals())
__SAGE_INTERACT_RESTART__
"""
# We cast the id to an integer, if it's an integer.
try:
cell_id = int(cell_id)
except ValueError:
pass
try:
S = state[cell_id]
# Look up the function that adapts inputs to have the right
# type
adapt_function = S["adapt"][adapt]
# Apply that function and save the result in the appropriate
# variables dictionary.
S["variables"][var] = adapt_function(value, globs)
except KeyError:
# If you change this, make sure to change notebook_lib.js as
# well.
print(INTERACT_RESTART)
def recompute(cell_id):
"""
Evaluates the :func:`interact` function associated to the cell
``cell_id``. This typically gets called after a call to
:func:`update`.
INPUT:
- ``cell_id`` - a string or an integer; the ID of an
:func:`interact` cell
EXAMPLES:
The following outputs __SAGE_INTERACT_RESTART__ to indicate that
not all the state of the :func:`interact` canvas has been set up yet
(this setup happens when JavaScript calls certain functions)::
sage: sagenb.notebook.interact.recompute(10)
__SAGE_INTERACT_RESTART__
"""
# We cast the id to an integer, if it's an integer.
try:
cell_id = int(cell_id)
except ValueError:
pass
try:
S = state[cell_id]
# Finally call the interactive function, which will use the
# above variables.
S['function']()
except KeyError:
# If you change this, make sure to change notebook_lib.js as
# well.
print(INTERACT_RESTART)
|