| { |
| "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." |
| } |
| ] |
| } |