File size: 39,575 Bytes
0b3f6d4 | 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 | {
"video_id": "dpJrM2_NOT8",
"language": "en",
"language_probability": 0.9993388056755066,
"segments": [
{
"start": 0.24,
"end": 4.24,
"text": "Welcome to Building UIs in the Terminal with Python Textual."
},
{
"start": 4.24,
"end": 7.24,
"text": "My name is Christopher and I will be your guide."
},
{
"start": 7.24,
"end": 11.24,
"text": "This course is all about the Textual Third Party Library,"
},
{
"start": 11.24,
"end": 15.24,
"text": "which you can use for building interactive programs in your terminal."
},
{
"start": 15.24,
"end": 18.24,
"text": "A program that uses Textual is known as an app."
},
{
"start": 18.24,
"end": 23.24,
"text": "Inside the app, you use different kinds of widgets to compose an interface."
},
{
"start": 23.24,
"end": 26.24,
"text": "Textual supports a wide variety of widgets,"
},
{
"start": 26.24,
"end": 29.24,
"text": "so this course will cover just some of the key ones"
},
{
"start": 29.24,
"end": 34.24,
"text": "like those used to display text, as well as buttons and user input."
},
{
"start": 34.24,
"end": 36.24,
"text": "Similar to their GUI cousins,"
},
{
"start": 36.24,
"end": 40.24,
"text": "terminal applications like those built with Textual are event-based."
},
{
"start": 40.24,
"end": 45.24,
"text": "Your code gets triggered based on an event like the user pressing a button."
},
{
"start": 45.24,
"end": 50.24,
"text": "Textual also has a way of writing less code for event handlers known as actions."
},
{
"start": 50.24,
"end": 54.24,
"text": "Both of these get covered in this course."
},
{
"start": 54.24,
"end": 58.24,
"text": "This course was tested using Python 3.13.5."
},
{
"start": 58.24,
"end": 61.24,
"text": "Python 3.14 was out at the time of recording,"
},
{
"start": 61.24,
"end": 64.24,
"text": "but Textual had not yet released a compatible version."
},
{
"start": 64.24,
"end": 68.24,
"text": "That said, I've used Textual fine with 3.14."
},
{
"start": 68.24,
"end": 73.24,
"text": "Only the debugging console had an issue due to the changes in how Async code works in 3.14."
},
{
"start": 73.24,
"end": 78.24,
"text": "The Textual libraries I used were the core 6.2.1"
},
{
"start": 78.24,
"end": 84.24,
"text": "and the development kit for debugging 1.7.0."
},
{
"start": 84.24,
"end": 86.24,
"text": "Textual is a tui framework."
},
{
"start": 86.24,
"end": 90.24,
"text": "That's tui for terminal user interface."
},
{
"start": 90.24,
"end": 92.24,
"text": "A tui is like a GUI,"
},
{
"start": 92.24,
"end": 95.24,
"text": "an interactive program made up of widgets."
},
{
"start": 95.24,
"end": 100.24,
"text": "Tuis achieved this by using terminal control sequences to position blocks and lines."
},
{
"start": 100.24,
"end": 105.24,
"text": "Of course, that means the interface is a little chunkier than a pixel-perfect GUI,"
},
{
"start": 105.24,
"end": 108.24,
"text": "but you'd be surprised just how much you can accomplish."
},
{
"start": 108.24,
"end": 111.24,
"text": "By focusing on the terminal, then being a Python library,"
},
{
"start": 111.24,
"end": 115.24,
"text": "your tui is more or less independent of the operating system."
},
{
"start": 115.24,
"end": 117.24,
"text": "Different OSs come with different terminals,"
},
{
"start": 117.24,
"end": 120.24,
"text": "so you may have restrictions on how many colors can be used at once,"
},
{
"start": 120.24,
"end": 125.24,
"text": "but for the most part, if you can open a terminal, you can use a textual application."
},
{
"start": 125.24,
"end": 130.24,
"text": "Textual is a robust framework coming with pretty much anything you need to build your tui,"
},
{
"start": 130.24,
"end": 132.24,
"text": "including a large number of widgets."
},
{
"start": 132.24,
"end": 136.24,
"text": "And proof of that is just how many textual applications are out there."
},
{
"start": 136.24,
"end": 140.24,
"text": "I've seen everything from a postman clone to a piano,"
},
{
"start": 140.24,
"end": 144.24,
"text": "but this course isn't about using textual apps, but building them."
},
{
"start": 144.24,
"end": 147.24,
"text": "You'll learn all about how to write and style an app,"
},
{
"start": 147.24,
"end": 151.24,
"text": "and how to compose widgets together to create your own tui."
},
{
"start": 151.24,
"end": 156.24,
"text": "Next up, you'll hit the ground running with a textual hello world."
},
{
"start": 156.24,
"end": 159.24,
"text": "In the previous lesson, I gave an overview of the course."
},
{
"start": 159.24,
"end": 164.24,
"text": "In this lesson, I'll show you how to get started with textual and write your first app."
},
{
"start": 164.24,
"end": 169.24,
"text": "You create a textual app by inheriting from textuals app class."
},
{
"start": 169.24,
"end": 174.24,
"text": "You subclass the app and override certain key methods to create an interface."
},
{
"start": 174.24,
"end": 179.24,
"text": "Then instantiate your class and call its run method to get it going."
},
{
"start": 179.24,
"end": 184.24,
"text": "Your app will be comprised of widgets, some of which are interactive like buttons"
},
{
"start": 184.24,
"end": 187.24,
"text": "and some which aren't like a text labels."
},
{
"start": 187.24,
"end": 191.24,
"text": "You tell textual what widgets you want by defining a compose method."
},
{
"start": 191.24,
"end": 195.24,
"text": "Inside of that, you yield the widgets you want to include."
},
{
"start": 195.24,
"end": 200.24,
"text": "Once you've done that, the built in run method takes care of everything else."
},
{
"start": 200.24,
"end": 205.24,
"text": "Since textual is a third-party library, you'll need to pip install to get it going."
},
{
"start": 205.24,
"end": 210.24,
"text": "As always, you should use a virtual environment when dealing with third-party libraries."
},
{
"start": 210.24,
"end": 214.24,
"text": "I won't show it here, but if you want to see what textual is capable of,"
},
{
"start": 214.24,
"end": 220.24,
"text": "you can execute the textual module using Python-m once you've got textual installed."
},
{
"start": 220.24,
"end": 226.24,
"text": "Running-m textual starts a demo application that shows off the different widgets and features."
},
{
"start": 226.24,
"end": 232.24,
"text": "Let me go open an editor and show you a textual hello world application."
},
{
"start": 232.24,
"end": 236.24,
"text": "These 11 lines are all you need for your first textual app."
},
{
"start": 236.24,
"end": 241.24,
"text": "It isn't particularly fancy, but hey, hello world isn't meant to be rocket surgery."
},
{
"start": 241.24,
"end": 247.24,
"text": "To build a textual app, you need to define a class that inherits from textuals app class."
},
{
"start": 247.24,
"end": 252.24,
"text": "The widgets module is where you find a rich set of widgets that you can use."
},
{
"start": 252.24,
"end": 253.24,
"text": "No pun intended."
},
{
"start": 253.24,
"end": 255.24,
"text": "You'll get that later."
},
{
"start": 255.24,
"end": 258.24,
"text": "There are a couple of different widgets for displaying text."
},
{
"start": 258.24,
"end": 263.24,
"text": "For your first app, I'm going to use the one known as static, as in static text."
},
{
"start": 263.24,
"end": 267.24,
"text": "As I've said, you declare an app by inheriting from textuals app class,"
},
{
"start": 267.24,
"end": 269.24,
"text": "which is what I've done here."
},
{
"start": 269.24,
"end": 273.24,
"text": "And then the compose method is where you declare your interface."
},
{
"start": 273.24,
"end": 276.24,
"text": "Whatever components you want are created here."
},
{
"start": 276.24,
"end": 279.24,
"text": "Note, this doesn't actually display the components."
},
{
"start": 279.24,
"end": 281.24,
"text": "It just registers them for later."
},
{
"start": 281.24,
"end": 288.24,
"text": "In a later lesson, I'll demonstrate how you have one more chance to do things to your widgets before the app gets displayed."
},
{
"start": 288.24,
"end": 291.24,
"text": "Textual treats the compose method as a generator."
},
{
"start": 291.24,
"end": 296.24,
"text": "When the app gets started, it uses the objects sent out of the method to create the interface."
},
{
"start": 296.24,
"end": 302.24,
"text": "If you've never played with generators before, you don't really have to understand how this works under the covers."
},
{
"start": 302.24,
"end": 307.24,
"text": "As long as you know, you need to yield each widget that you want in your interface."
},
{
"start": 307.24,
"end": 311.24,
"text": "The only widget in this first app is the static text widget."
},
{
"start": 311.24,
"end": 316.24,
"text": "Here, I'm constructing a new instance, passing in the text I want to display,"
},
{
"start": 316.24,
"end": 320.24,
"text": "sticking with tradition, and that text is hello world."
},
{
"start": 320.24,
"end": 327.24,
"text": "As this script is both declarative and runnable, I'm nesting the running part inside of a dundermain check."
},
{
"start": 327.24,
"end": 334.24,
"text": "If you've never seen this before, dundermain gets set to dundermain when your script is run from the command line."
},
{
"start": 334.24,
"end": 339.24,
"text": "If this file were merely imported, this if clause wouldn't get triggered."
},
{
"start": 339.24,
"end": 342.24,
"text": "For a textual app, this isn't really all that important."
},
{
"start": 342.24,
"end": 347.24,
"text": "You're almost always using it as a script, but I'm sticking with best practices here."
},
{
"start": 347.24,
"end": 352.24,
"text": "To run your toy, you simply need to instantiate your new app class,"
},
{
"start": 352.24,
"end": 355.24,
"text": "and then call its inherited run method."
},
{
"start": 355.24,
"end": 361.02,
"text": "Let's try this out, running the code."
},
{
"start": 361.02,
"end": 364.02,
"text": "And there you go. You've said hello."
},
{
"start": 364.02,
"end": 370.02,
"text": "There's not much to see here, but notice a subtle difference from, say, just printing out the string."
},
{
"start": 370.02,
"end": 374.02,
"text": "If you had printed the string out, the text would have appeared below the execution line."
},
{
"start": 374.02,
"end": 379.02,
"text": "The reason hello world is at the top here is because textual clears the terminal,"
},
{
"start": 379.02,
"end": 382.02,
"text": "and the application takes over the terminal window."
},
{
"start": 382.02,
"end": 387.02,
"text": "For me, this is in my sub window below the line, but it's taking over everything it can control."
},
{
"start": 387.02,
"end": 391.02,
"text": "To exit a textual application, you press control queue."
},
{
"start": 391.02,
"end": 394.02,
"text": "I actually don't really like that, and we'll show you how to change it later."
},
{
"start": 394.02,
"end": 398.02,
"text": "But for now, let me control queue to get out of here."
},
{
"start": 398.02,
"end": 400.02,
"text": "Okay, you've seen your first app."
},
{
"start": 400.02,
"end": 406.02,
"text": "In the next lesson, I'll dive a little deeper into displaying text widgets on the screen."
},
{
"start": 406.02,
"end": 411.02,
"text": "In the previous lesson, I showed you the hello world version of a textual application."
},
{
"start": 411.02,
"end": 414.02,
"text": "In this lesson, I'll play around a little more with text widgets"
},
{
"start": 414.02,
"end": 418.02,
"text": "and show you some of the styling you can apply to them."
},
{
"start": 418.02,
"end": 422.02,
"text": "Every displayable component of your two-way application is a widget."
},
{
"start": 422.02,
"end": 427.02,
"text": "So if you want text on the screen, you can't just print it. You need a text widget."
},
{
"start": 427.02,
"end": 433.02,
"text": "The two most common text widgets are static, which you saw in the previous lesson, and label."
},
{
"start": 433.02,
"end": 435.02,
"text": "For the most part, they're similar to each other."
},
{
"start": 435.02,
"end": 438.02,
"text": "They just behave slightly differently when laid out."
},
{
"start": 438.02,
"end": 441.02,
"text": "Label widgets, by default, are the size of the text they represent."
},
{
"start": 441.02,
"end": 445.02,
"text": "While static widgets take up the space of their holding container,"
},
{
"start": 445.02,
"end": 448.02,
"text": "you'll see the difference in a sec when I give you a demo."
},
{
"start": 448.02,
"end": 453.02,
"text": "The creators of textual are also the creators of the rich text library."
},
{
"start": 453.02,
"end": 457.02,
"text": "Like with rich, the text contents can be specified using a markup language."
},
{
"start": 457.02,
"end": 465.02,
"text": "Most text you type is just text, but you can put styling commands inside of square brackets to change how the text is displayed."
},
{
"start": 465.02,
"end": 471.02,
"text": "For example, square brackets, bold, red, changes the text to bold font and colored red."
},
{
"start": 471.02,
"end": 478.02,
"text": "All widgets have a styles property, which allows you to programmatically get and set the style of the widget."
},
{
"start": 478.02,
"end": 483.02,
"text": "The styles property itself has properties, which are the things you change."
},
{
"start": 483.02,
"end": 490.02,
"text": "Some common ones are background, which controls the widget's background color, border for specifying an outline,"
},
{
"start": 490.02,
"end": 497.02,
"text": "text a line for centering or write aligning text within a widget, padding for determining the spacing within the widget,"
},
{
"start": 497.02,
"end": 501.02,
"text": "and margin for spacing around the widget."
},
{
"start": 501.02,
"end": 506.02,
"text": "If you're familiar with CSS on the web, these styles might be ringing a bell."
},
{
"start": 506.02,
"end": 512.02,
"text": "Textual uses a subset of CSS to control appearances, and so if there's some styling you can do in CSS,"
},
{
"start": 512.02,
"end": 515.02,
"text": "you'll probably find it in textual as well."
},
{
"start": 515.02,
"end": 521.37,
"text": "In a regular Python script, the code gets executed line by line."
},
{
"start": 521.37,
"end": 525.37,
"text": "If I have three print statements in a program, they run exactly in order."
},
{
"start": 525.37,
"end": 528.37,
"text": "Of course, when you start building functions, this changes a little."
},
{
"start": 528.37,
"end": 532.37,
"text": "The declaration order of the functions isn't necessarily the running order."
},
{
"start": 532.37,
"end": 539.37,
"text": "Twees and guis are similar. You typically want code to fire when the user does something to the interface."
},
{
"start": 539.37,
"end": 543.37,
"text": "How most twis and guis accomplish this is with an event system."
},
{
"start": 544.37,
"end": 550.37,
"text": "You create an event handler by declaring a function or method and register it against that event."
},
{
"start": 550.37,
"end": 556.37,
"text": "When things happen in the two events fire, and any associated handlers get called at that time,"
},
{
"start": 556.37,
"end": 562.37,
"text": "typical events are things like the user pressing a key or clicking a button."
},
{
"start": 562.37,
"end": 565.37,
"text": "There are also system type events that aren't user triggered."
},
{
"start": 565.37,
"end": 571.37,
"text": "For example, textual fires events when a widget gets put into or removed from the interface."
},
{
"start": 572.37,
"end": 576.37,
"text": "This allows you to dynamically change things during the execution of your program."
},
{
"start": 576.37,
"end": 580.37,
"text": "Textual gives you several ways of declaring event handlers."
},
{
"start": 580.37,
"end": 584.37,
"text": "The first one I'm going to show you is the built-in on prefix."
},
{
"start": 584.37,
"end": 589.37,
"text": "For example, the on key method gets called for each key press."
},
{
"start": 589.37,
"end": 593.37,
"text": "While the on-mount method gets called when the application mounts the widgets."
},
{
"start": 593.37,
"end": 600.37,
"text": "This is after the composed method fires, allowing you one last chance to do something with your widget before it gets shown."
},
{
"start": 601.37,
"end": 605.37,
"text": "Let's go play with static and label widgets some more."
},
{
"start": 605.37,
"end": 610.37,
"text": "This new app has two text widgets in it, one static and one label."
},
{
"start": 610.37,
"end": 617.37,
"text": "I'm going to get a little fancier than hello world and show you some of the things you can do to style your text."
},
{
"start": 617.37,
"end": 624.37,
"text": "In the previous lesson, you saw how you have to yield a widget inside of the composed method to register it with textual."
},
{
"start": 624.37,
"end": 631.37,
"text": "That's required, but you can create and store a reference to the widget before yielding it, like I'm doing here."
},
{
"start": 631.37,
"end": 634.37,
"text": "This allows me to access the widget later on in the code."
},
{
"start": 634.37,
"end": 639.37,
"text": "I'm instantiating this static widget with some textual markup."
},
{
"start": 639.37,
"end": 643.37,
"text": "Inside of the string, I've got square bracket style indicators."
},
{
"start": 643.37,
"end": 646.37,
"text": "This will turn the text bold and red."
},
{
"start": 646.37,
"end": 653.37,
"text": "That style applies to this word and then square brackets with a slash are what turns it off."
},
{
"start": 653.37,
"end": 656.37,
"text": "Anything after this closing tag will be back to normal."
},
{
"start": 656.37,
"end": 659.37,
"text": "When you close the tag, you don't have to turn everything off."
},
{
"start": 659.37,
"end": 665.37,
"text": "I could have turned off just the bold and then the rest of the sentence would still be red if I had wanted."
},
{
"start": 665.37,
"end": 671.37,
"text": "Once I've constructed the object and kept a reference to it, I yield the reference to include it in the interface."
},
{
"start": 671.37,
"end": 675.37,
"text": "Here, I've done something similar with a label widget."
},
{
"start": 675.37,
"end": 678.37,
"text": "Note to the use of the closing shortcut here."
},
{
"start": 678.37,
"end": 682.37,
"text": "Square bracket slash means to close all open markup tags."
},
{
"start": 682.37,
"end": 687.37,
"text": "So this will turn off the yellow and italic and that means you don't have as much typing to do."
},
{
"start": 687.37,
"end": 694.37,
"text": "When you run a textual application, it calls your apps compose method to determine what to show on the screen."
},
{
"start": 694.37,
"end": 697.37,
"text": "When it's ready to go, it fires the mount event."
},
{
"start": 697.37,
"end": 703.37,
"text": "This is done in two steps because some of the properties of widgets aren't known until all the widgets have been created."
},
{
"start": 704.37,
"end": 709.37,
"text": "Depending on how you're laying things out, the size of some widgets can affect the size of others."
},
{
"start": 709.37,
"end": 718.37,
"text": "The mount event is fired after all this has been figured out and gives you a last chance to modify the properties of your widgets before they get displayed to the screen."
},
{
"start": 718.37,
"end": 725.37,
"text": "For the core app events, you can register an event handler simply by overloading their on method in the app."
},
{
"start": 725.37,
"end": 730.37,
"text": "Here on mount is what gets called when the mount event fires."
},
{
"start": 731.37,
"end": 736.37,
"text": "And this is why I kept a reference to that static widget that I built in the compose."
},
{
"start": 736.37,
"end": 741.37,
"text": "It means I can access it here and make changes after the compose is run."
},
{
"start": 741.37,
"end": 748.37,
"text": "The first change I'm implying is by setting the background property on the styles property, making our widget blue."
},
{
"start": 748.37,
"end": 751.37,
"text": "The border property takes a tuple."
},
{
"start": 751.37,
"end": 759.37,
"text": "The first value in the tuple describes the style of the border where solid means a solid line and the second value specifies the color of the border."
},
{
"start": 760.37,
"end": 764.37,
"text": "Setting text align to center centers the text in the widget."
},
{
"start": 764.37,
"end": 771.37,
"text": "If you've done CSS before, be careful to note the difference here. Python doesn't support hyphens in property names."
},
{
"start": 771.37,
"end": 776.37,
"text": "So anywhere where you would use a hyphen in CSS, you need to change it to an underscore."
},
{
"start": 776.37,
"end": 782.37,
"text": "This will get a little confusing later, and I muck this up all the time, but you have to kind of keep it in mind."
},
{
"start": 782.37,
"end": 786.37,
"text": "The padding properties specifies the spacing within the widget."
},
{
"start": 786.37,
"end": 796.37,
"text": "The value of one one here says to put a one line space above and below the text as well as a one character space to the left and right of the text."
},
{
"start": 796.37,
"end": 802.37,
"text": "Other parts of the layout may cause there to be more padding than this, so consider this a minimum."
},
{
"start": 802.37,
"end": 812.37,
"text": "The margin property specifies the spacing around the widget for four means four lines above and below and four characters to the left and right of the widget."
},
{
"start": 812.37,
"end": 817.37,
"text": "Again, the layout could make it bigger than this, so once more it's a minimum."
},
{
"start": 817.37,
"end": 826.37,
"text": "Similar to the styling for the static widget here, I style the label this time making the background color dark green and using a double lined mortar in red."
},
{
"start": 826.37,
"end": 832.54,
"text": "When you press a key on your keyboard, textual fires the key event."
},
{
"start": 832.54,
"end": 838.54,
"text": "You can override the default on key method to do what you like with those key presses."
},
{
"start": 838.54,
"end": 845.54,
"text": "Here, I'm using a pattern matching block to react to the key property on the event object passed into the handler."
},
{
"start": 845.54,
"end": 852.54,
"text": "If you haven't seen these before, they were introduced to Python 310 and they serve a similar purpose to if else blocks."
},
{
"start": 852.54,
"end": 860.54,
"text": "The argument to the match statement serves as a value to look for, then the case statement works like an if equals statement."
},
{
"start": 860.54,
"end": 867.54,
"text": "If the value inside of event key is q, then this case runs, and so the exit function will get called."
},
{
"start": 868.54,
"end": 875.54,
"text": "In the previous lesson, I mentioned I don't really like control q to quit, and so now I've got it set so pressing q will be enough."
},
{
"start": 875.54,
"end": 880.54,
"text": "Below this method, I've instantiated the app and called its run just like before."
},
{
"start": 880.54,
"end": 882.54,
"text": "Let's try this out."
},
{
"start": 882.54,
"end": 889.5,
"text": "And here you have the app."
},
{
"start": 889.5,
"end": 894.5,
"text": "Static widgets fill the container, which in this case is most of the screen."
},
{
"start": 894.5,
"end": 900.5,
"text": "It's actually all of the screen, but the margin property of the widget is keeping a four character boundary all around it."
},
{
"start": 900.5,
"end": 904.5,
"text": "The label widget by default is only the size of its contents."
},
{
"start": 904.5,
"end": 910.5,
"text": "Of course, the padding within it and the border property affected size, but it doesn't get any bigger than that."
},
{
"start": 910.5,
"end": 914.5,
"text": "It also has a margin around it, which is why it's offset."
},
{
"start": 914.5,
"end": 920.5,
"text": "And since I added the key event handler, I can press q to quit."
},
{
"start": 920.5,
"end": 926.5,
"text": "Next up, we're on styling and how to use CSS to control your app's appearance."
},
{
"start": 927.5,
"end": 932.5,
"text": "In the previous lesson, I showed you how to style text widgets using the styles property."
},
{
"start": 932.5,
"end": 937.5,
"text": "In this lesson, I'll show you the power of textual CSS mechanism."
},
{
"start": 937.5,
"end": 943.5,
"text": "Textual supports a subset of the cascading style sheet interface used to style web pages."
},
{
"start": 943.5,
"end": 946.5,
"text": "They call the subset T CSS."
},
{
"start": 946.5,
"end": 950.5,
"text": "Although I do a lot of web dev, I'm not really a fan of CSS."
},
{
"start": 950.5,
"end": 958.5,
"text": "It feels like a Jenga tower of hacks to me with each release trying to compensate for the fact that the last release didn't fully compensate for the one before it."
},
{
"start": 958.5,
"end": 962.5,
"text": "Textuals use of CSS is why it took me a while to even try it out."
},
{
"start": 962.5,
"end": 966.5,
"text": "But the library builders have chosen the right parts of CSS."
},
{
"start": 966.5,
"end": 971.5,
"text": "In the previous lesson, you saw how to use the styles property to individually change a widget."
},
{
"start": 971.5,
"end": 976.5,
"text": "But by using CSS, you can achieve the same thing with far less code."
},
{
"start": 976.5,
"end": 983.5,
"text": "Like with the web, you can specify CSS separately from the components and then the styles get applied to your widgets."
},
{
"start": 983.5,
"end": 995.5,
"text": "You can specify style rules based on the Python class of the widget, the ID associated with a widget instance, or by creating a CSS class with a string."
},
{
"start": 995.5,
"end": 1002.5,
"text": "The first and last example of these mean you can create styles that are consistent across your interface for multiple widgets."
},
{
"start": 1002.5,
"end": 1009.5,
"text": "For example, changing all buttons or all of the widgets with a certain CSS class label."
},
{
"start": 1009.5,
"end": 1013.5,
"text": "One of the things I still don't like about CSS is debugging it."
},
{
"start": 1013.5,
"end": 1016.5,
"text": "I never seem to get the layout right the first time."
},
{
"start": 1016.5,
"end": 1018.5,
"text": "Textual helps with this as well."
},
{
"start": 1018.5,
"end": 1021.5,
"text": "So far, you've been running your apps directly from Python."
},
{
"start": 1021.5,
"end": 1025.5,
"text": "But you can also use the textual command to run them instead."
},
{
"start": 1025.5,
"end": 1033.5,
"text": "To do and use the dash dash dev flag, textual will update the interface on the fly while you edit the CSS."
},
{
"start": 1033.5,
"end": 1039.5,
"text": "You don't have to exit and restart the program, you simply make the change and watch its effects."
},
{
"start": 1039.5,
"end": 1045.5,
"text": "To use this feature, you need a separate library, textual dev, you pip install it like any other."
},
{
"start": 1045.5,
"end": 1053.5,
"text": "Then once you have, you can invoke your app either with Python directly or through the textual run command, like I'm showing here."
},
{
"start": 1053.5,
"end": 1059.5,
"text": "Let's go right another textual app this time using CSS to style it."
},
{
"start": 1059.5,
"end": 1064.5,
"text": "I'm still sticking with static widgets, but this time I'm going to style them with CSS."
},
{
"start": 1064.5,
"end": 1068.5,
"text": "There are several ways of associating T CSS with your app."
},
{
"start": 1068.5,
"end": 1077.5,
"text": "The one I'm using here is setting the CSS path class property, giving it the name of the file where I'm going to define my CSS rules."
},
{
"start": 1078.5,
"end": 1083.5,
"text": "Like before, I'm yielding components, this one being a vanilla static object."
},
{
"start": 1083.5,
"end": 1089.5,
"text": "And here I'm yielding another one, but this time I've set the ID argument."
},
{
"start": 1089.5,
"end": 1094.5,
"text": "Now this object can be associated with ID based CSS rules."
},
{
"start": 1094.5,
"end": 1098.5,
"text": "Here, instead, I'm declaring a CSS class."
},
{
"start": 1098.5,
"end": 1102.5,
"text": "You do that with the class's argument, which takes a string."
},
{
"start": 1102.5,
"end": 1107.5,
"text": "This object will respond to CSS class rules named static CLS1."
},
{
"start": 1107.5,
"end": 1111.5,
"text": "A widget can have multiple CSS classes."
},
{
"start": 1111.5,
"end": 1114.5,
"text": "You do that by passing in a space delimited string."
},
{
"start": 1114.5,
"end": 1122.5,
"text": "This object has both the static CLS1 class, like the one above it, and a static CLS2 as well."
},
{
"start": 1122.5,
"end": 1126.5,
"text": "Now let's look at the associated T CSS file."
},
{
"start": 1128.98,
"end": 1131.98,
"text": "A CSS file is comprised of rules."
},
{
"start": 1131.98,
"end": 1135.98,
"text": "You start by specifying what objects the rules apply to."
},
{
"start": 1135.98,
"end": 1141.98,
"text": "Here, the capital S static means this style applies to all widgets of that type."
},
{
"start": 1141.98,
"end": 1147.98,
"text": "You then use brace brackets to denote the block of style properties to apply to objects that meet this rule."
},
{
"start": 1147.98,
"end": 1154.98,
"text": "All of our static widgets will have a blue background, and one line and character padding around them."
},
{
"start": 1154.98,
"end": 1160.98,
"text": "Hashmark, also known as octathorp, if you're not on a first name basis, indicates an ID."
},
{
"start": 1160.98,
"end": 1166.98,
"text": "So hash static with ID means apply this rule to the widget that has this ID."
},
{
"start": 1166.98,
"end": 1171.98,
"text": "And the style I want to apply to the object is to give it a white border."
},
{
"start": 1171.98,
"end": 1175.98,
"text": "A period means this rule applies to a CSS class."
},
{
"start": 1175.98,
"end": 1179.98,
"text": "All objects with this CSS class get these rules."
},
{
"start": 1179.98,
"end": 1185.98,
"text": "The C in CSS stands for cascading, which in this case means a sort of inheritance."
},
{
"start": 1185.98,
"end": 1190.98,
"text": "The widget that this class applies to is also a static widget."
},
{
"start": 1190.98,
"end": 1193.98,
"text": "So the top rule and this rule will apply."
},
{
"start": 1193.98,
"end": 1199.98,
"text": "The rule about an object's CSS class takes precedence over the rule about the Python class."
},
{
"start": 1199.98,
"end": 1205.98,
"text": "So objects of this CSS type have the background property of blue overwritten to become green."
},
{
"start": 1205.98,
"end": 1210.98,
"text": "You'll recall that one of our widgets has both of our CSS classes applied."
},
{
"start": 1210.98,
"end": 1218.98,
"text": "So that one will have both a green background from the other class rule and a dashed yellow border from this one."
},
{
"start": 1218.98,
"end": 1221.98,
"text": "Let me fire this up in a terminal so you can take a look."
},
{
"start": 1221.98,
"end": 1226.51,
"text": "Okay, I've got my editor open on the left."
},
{
"start": 1226.51,
"end": 1229.51,
"text": "Now let me run the app using the dev flag."
},
{
"start": 1229.51,
"end": 1237.86,
"text": "As you can see, our four widgets have four different styles."
},
{
"start": 1237.86,
"end": 1246.86,
"text": "This static CSS rule applies to all of the widgets, making this widget and this widget have a blue background."
},
{
"start": 1246.86,
"end": 1250.86,
"text": "The other two don't because other rules override it."
},
{
"start": 1250.86,
"end": 1259.86,
"text": "Our third and fourth widgets have the static class one CSS class applied, which is what overrides the blue and turns them green."
},
{
"start": 1259.86,
"end": 1266.86,
"text": "The fourth widget also has this static CLS to applied, which is what gives you the yellow border."
},
{
"start": 1266.86,
"end": 1278.95,
"text": "Since I ran this app using the textual run command in dev mode, I can live edit this style sheet."
},
{
"start": 1278.95,
"end": 1283.95,
"text": "As soon as I save this, the app on the right hand side gets updated."
},
{
"start": 1283.95,
"end": 1285.95,
"text": "This is great for debugging."
},
{
"start": 1285.95,
"end": 1293.95,
"text": "The very common trick when you're trying to figure out if rules are getting applied is to add a red border around the thing you're trying to debug."
},
{
"start": 1293.95,
"end": 1297.95,
"text": "This often also makes the padding and the margins clearer as well."
},
{
"start": 1297.95,
"end": 1304.95,
"text": "By being able to do it live, you can muck around until you understand what is going on and make whatever fixes you want."
},
{
"start": 1304.95,
"end": 1309.49,
"text": "So far, you've only seen widgets that do nothing."
},
{
"start": 1309.49,
"end": 1314.49,
"text": "In the next lesson, I'll show you a couple of widgets with which your user can interact."
}
]
} |