PyBay 2024 - Scrolling Animated ASCII Art in Python
Al Sweigart discusses scroll art at the PyBay 2024 conference in San Francisco on September 21, 2024. This is similar to the PyTexas 2024 talk. The talk was recorded and is available on pyvideo.org.
Here is an imperfect, machine-generated transcript:
Okay, so I'm going to give a talk on scroll art, but I thought I'd just start off by showing you what it is.
This is about 2,000 lines of Python code. The output is all to this terminal window, which I'll be controlling from this other terminal window right here.
Using a live demo music video of procedurally generated code—what could possibly go wrong?
Yes, the music is The Forbidden Zone, composed by Danny Elfman in the early 1980s for a cult classic movie of the same name. There are some light flashing elements, just in case you're sensitive to that.
Thank you. Lights, please.
[music plays]
Okay, lights please. Ah, thank you. Hello. Okay.
I like to make a visual impression. In fact, there's a lot of nice slides in this talk, so you might want to have your camera phone ready, if you're the type of person who likes to take photos and post them to social media. Only if you're that type of person; I'm not asking. But be sure to put hashtag PyBay if you do.
Okay, talk begins now.
Hello. Thanks for having me here, and thank you to all of the PyBay organizers and volunteers who make this conference not only possible, but wonderful.
There's a lot in these slides, but you can download the slides later from bit.ly slash PyBay 2024 scroll art, and you can see the art at scrollart.org.
It's great to be here in the beautiful Bay Area on this, our most sacred of holy days: the 21st of September.
I'm Al Swigert. I'm mostly known as the author of Automate the Boring Stuff with Python: Practical Programming for Total Beginners. It's a programming book for total beginners. It's like, what is the minimum amount of coding an office worker needs to be productive with a computer? So, computer programming as a trade rather than a science.
I've also written a few other books. Somehow, I found myself going from a kid who wanted to program Zelda, to a computer science student, to a software engineer adult, and now I'm someone who spends a lot of time thinking about how to get people interested in coding, especially with an approachable language like Python, which is really what this talk is about.
You can read all of these books for free at inventwithpython.com under a Creative Commons license. I'd like to thank my publisher, No Starch Press, for having the foresight to allow me to publish my books under a Creative Commons license. It's helped me generate more word of mouth more than anything else.
And I'd also like to thank all librarians for the great amount of work they do sharing books with everyone. Thank you. Yes.
These are interesting times, and if nonprofit institutions like libraries, or Wikipedia, or the Internet Archive didn't exist, I'm not sure if we would create them. And if we lose them, I'm not sure if we'd get them back. I appreciate them. I support them. I do not take them for granted.
So, on the left is an artist who fills glass fish bowls with different-colored sand and pushes the grains into place to draw portraits. In the center is an artist who arranges beads on a flat board and films herself in slow motion tossing them upwards. On the right is an artist who carves patterns into the skin of a watermelon, getting different colors as they carve deeper.
On the left is an artist who draws portraits only tracing along the edge of a coin. In the center is an artist who completes their illustrations using the shadow cast by common household objects. On the right is an artist who has several elaborate bubble-making tools.
A good friend of mine once told me that not only will humans continue to create new art, but that there will always be new forms of art.
So I'd like to talk about something I call scroll art.
Scroll art is the text output of a command-line program that scrolls upward as the program continues to generate text. It's easy for beginners to create. You really only need to understand the print function and loops to make it, but it's also fun for experienced developers to explore. It's kind of a "minutes to learn, lifetime to master" sort of thing.
It's sort of like an animated form of ASCII art. But, well, actually, it's easier to show you.
Here's some Python programs, but to render them in the browser, I had to port them to JavaScript. I believe there's a PyScript talk after this one in this very room.
So this is Zigzag. It's incredibly simple, and from the output, if you're a programmer, you can easily figure out how to make it.
There's just a string of asterisks, and it starts off with zero indentation, and then one space of indentation, then two spaces of indentation, and keeps increasing over and over again until it reaches a maximum. And then it starts reducing the amount of indentation, and it produces this animated zigzag pattern.
Really, a lot of the primary concern that people have when they're first learning to code is they think that coding means you have to know a lot about math. And that's kind of true and kind of not true.
Really, I think a lot of it comes down to: can you identify patterns and sort of reverse-engineer them and figure out how to express them as code?
And so, looking at this, you can kind of see, like, "Oh, right. I see the pattern here. We're increasing the spaces and then decreasing the spaces."
This next one is called Star Field. It's random. It uses random numbers, so it's randomly generated. It's never the same each time.
But you can see that the density of the asterisks in the star field continues to increase until it reaches 100%, and it's just a straight field of asterisks. And then it starts decreasing to become more and more sparse and empty, and then it just repeats in that cycle.
Now, a quarter century ago, there was a movie that came out called The Matrix, and it had this iconic visual of sort of like raindrop alphanumerics falling down the screen.
Of course, with scroll art, we can only scroll things upwards. But I thought, "Hey, that's pretty cool. I can make that as scroll art as well."
And this I call Proton Stream, because it just reminds me of a Ghostbusters proton pack. These are just characters that are doing a random left-right walk, but with the one constraint that they always have to be within a certain amount of space of each other. And I really quicken the speed of the scrolling to give it a sort of frenetic energy right here.
And then there's this one. This one's kind of hard to explain, so I'm just going to go ahead and pause this.
Okay, so each column has a binary state. It's either going to be a period or it's an at symbol. And randomly, I'll have what I call a toggler that appears, and it's either going to start moving left or right, toggling the state of that column.
So this starts forming a little triangle down the side. And, of course, when two togglers cross paths, they cancel each other out.
But the end effect is that you have this really weird emergent behavior that's kind of fun to look at, even though the actual code for this is really small.
In fact, the code for all of these is usually just, like, I don't know, less than 50 lines of code. This is something that I really strive for, as far as making this something that's approachable for beginners.
So you've seen scroll art, but let's go ahead and define it.
Scroll art is similar to something called ASCII art, which was popular in the '90s as a way to make art out of text characters. Here's a cat and a dolphin by prolific ASCII artist Joan G. Stark.
Scroll art is made out of monospace text characters by a program that's displayed in a terminal window. So all the characters are the same width, and it's animated when the terminal window fills up with text and the text starts scrolling upwards.
So it's like if you were looking at a static text file and you just kept hitting the down arrow key over and over again.
The main limitations of scroll art are that you can't move the cursor arbitrarily around the screen, and you can't erase text once it's been printed. But even within those constraints, there is a wide variety of creativity that you can employ.
So I've posted my examples at scrollart.org.
Now, if you are like me and of a certain age, you might remember this as one of your first programs that you ever wrote in a programming language called BASIC.
I've named this scroll art, but I didn't really invent it. This has been done before.
This program just prints "Hello" to the screen, and then the second instruction tells it to go back to the first instruction. So it prints it again and again, and then your screen just fills up with the word "Hello."
The program would only stop once you pressed Control-C. But back then, I didn't know about Control-C, so the way I stopped infinite loops was just turning off the computer and then turning it back on again.
So.
But I want to stress that scroll art is not meant to be some kind of retro-computing thing.
You know, a lot of people have a dim view of nostalgia, and I am one of those people. I'm interested in teaching people to code here in the 21st century.
I don't want to make that mistake that, "Oh, people today will want to learn how to code the way I learned how to code when I was younger and didn't have to worry about mortgages."
So the text-based nature of scroll art programs—it's not supposed to be some sort of old-school throwback. It's a constraint that manages expectations and makes programming approachable.
So it's computer art, but without the complicated shaders and 3D vectors or AI-generated art or anything like that.
So much of computing today is this magical black box, and it's really hard to connect how the code we write translates to what we see on the screen.
But with scroll art, scroll art is text and the source code is text. So it really gives you this sense of cause and effect, to be able to point to the print function call and say, "Ah, this is the line of code that produces this line of output that I see."
So this makes scroll art a great way to teach programming. But more importantly, sticking to text means that any computer can do scroll art. You don't need a modern laptop or a high-end graphics card.
I even ported one of the scroll art programs I made to a literal Apple IIe.
These photos are from a place called the Recurse Center in New York. RC is a 12-week program. It's sort of like an artist retreat, except for software developers.
I first heard about it here at PyCon several years ago, but I only finally had time this year to apply. So it's where I started making scroll art, sort of out of procrastination from the project that I was supposed to be working on.
Anyway, but I just want to show that scroll art is something that we could have been making 40 years ago. And I'm sure some people did, but we just didn't have the internet and social media to spread ideas like this.
We didn't have conferences like PyCon that were a lot more accessible and easy to attend. Or, you know, maybe you're watching this talk in the future on YouTube, if that website is still around.
But, you know, it was hard to spread these ideas of cool things you can do with computers.
And every programming language can print text, but Python is especially suited for scroll art. It has a simple, readable syntax that's good for beginners. Loops are straightforward. It has a rich set of features for string manipulation and easily handles Unicode characters for non-English text.
That's a major limitation of ASCII, the character set. That's the "A" in ASCII: American characters. Literally American characters only, because it has a dollar sign, but it doesn't have a British pound symbol.
Scroll art is also great for procedural generation, and Python's random module is really straightforward when it comes to getting random integers or setting the random seed value.
And you control the rate of scrolling by introducing pauses, so time.sleep is really straightforward to use for that.
And again, scroll art isn't a new technology, so all the things that already make Python great to use make it great to use for scroll art.
So sticking to text also means that you don't need to install additional libraries, and that's a really big one.
If you've ever had a Python workshop, you've probably had to have a pre-workshop workshop just to get Python installed on everyone's computer and set up, and also install all the third-party packages that they're going to be using.
But scroll art just uses the print function and also loops. So this means that you can do scroll art even in browser-based REPLs or interactive shells, which means you can do scroll art programming from tablets and smartphones.
You don't have to invent anything new. You can just use existing tools and platforms.
Now, the tech industry loves to talk about radical innovation and revolutionizing our world, especially when it comes to education. But a lot of what the tech industry produces assumes a certain level of affluence or a certain level of expertise.
A big part of Python's success has been making a free, open-source programming language that is approachable to people outside of the software field.
Now, don't get me wrong. I think there's a lot of great stuff out there to get people, especially young people, into tech. But these things can't get everyone into tech.
We need something that scales, not because it is fancy and high-tech, but something that scales because it's boring and simple.
So scroll art is not about retro computing.
When I say that any computer can do scroll art, I'm not thinking about PCs from the '80s. I'm talking about the tablets and Chromebooks and old desktops that parents have gathered for an after-school computer club.
Or, I mean, the computers that are used by career switchers who don't want to drop a lot of money when they're just starting out.
So that's what scroll art is. But why should you care?
So I have an interest in programming education, which is mostly about getting people interested in programming.
Some of that is teaching concepts, but it's mostly about generating enthusiasm. And most beginners want to make things.
The worst feeling for beginners is they master all of these abstract concepts, and then they turn to face a blank editor and they just don't know how to write a program. They don't know how to apply all the things that they've learned.
Beginners don't really have a lot of knowledge to make big things, but they have a little bit of knowledge to make small things. And small things can still be interesting.
I teach Python because Python is so widespread, but I need programming projects that I can really use in any language without a lot of setup, and also no matter what hardware they have access to.
But let's say that you're a senior software engineer with the latest MacBook. Scroll art is still something for you.
Scroll art projects are great practice when you're learning a new programming language and you just need some small, well-defined projects to write.
But even in languages that you're well-versed in, scroll art's limitations force you to apply your wide skill set in very specific ways.
And there are endless ways to make scroll art. So let's examine some of them.
First, I'm going to say something incredibly pretentious: scroll art creates the illusion—the illusion of movement and infinity.
Ooh, wow. That's really deep.
Here's what I mean.
Scroll art is animated, but you aren't writing any animation code. You're just calling the print function over and over again.
But you've seen it. This looks like upward movement. And if you add in random numbers, the output never repeats. It's sort of like the digits of pi.
And if you put all of that into an infinite while loop, it just goes on forever.
So it's like a lava lamp. It moves. It never repeats. But it has this simplicity, and it's kind of neat to stare at.
So those are the kinds of programming projects that you can make with scroll art.
So here's some different types.
There are periodic scroll art programs that do a simple repetition. Zigzag is an example of that. It's not actually moving, but it has the illusion of movement. That's a great program to start off with.
But if you want to get a little bit more complicated, and you know about the math.sin function, you can add a little sine-wave version of that zigzag.
I don't think I've ever used the mathematical sine function except when making silly art projects, by the way. Twenty years of a career: never use sine and cosine.
Another one that I like to start people off with is this periodic program just called Spike. And it just produces this long spike over and over again.
I kind of watch this, and I get this, like, rhythmic thumping noise in my head when I stare at it for too long.
Here's something even simpler.
I call this wallpaper scroll art because these are just static images that repeat over and over again. It's a lot of fun if you're just, like, playing around with repeating ASCII-art images.
This is actually the carpet pattern from the Overlook Hotel from the movie The Shining. But I'd also just play around with, like, little hexagons. The forward and backward slashes are great for that, little brick tiles, and a bunch of little miniature skulls flying around.
And if you need new inspiration for new patterns, you can always just do a Google Image search for tessellation or tiles. You get tons of good results.
It helps to change the search settings to find just black-and-white images or line-art images.
Not everything's going to be useful, but I almost always get a handful of ideas just by sifting through existing artwork.
Not just that, but I also started doing searches for Islamic geometric art. There's an entire history of art here that you can learn about.
This whole process has been kind of incredible because I'll start off by, like, trying to figure out easy programming projects for beginners, and then I'm on the Wikipedia page for the Iberian Peninsula.
There's a traditional form of Japanese embroidery called sashiko that involves white threads on indigo-dyed fabric to create these beautiful geometric patterns. So that's another great source of inspiration for scroll art.
But ever since I've started looking at this, I kind of started looking at the world differently.
These are just paver stones in Prospect Park in Brooklyn, and I just was walking down, staring at the floor, like I do when I'm walking around. And the hexagon patterns are neat, but also just the individual cracks there.
And so now—I haven't finished this—but I'm working on this sort of, like, lightning-cracked algorithm involving underscores and slash ASCII-art characters.
Now, wallpaper scroll art has this static look, but you can start introducing variations to the patterns with random numbers.
The main example I have here is Ducklings.
So this program goes through various combinations of ducklings. They're either facing left or right, and their mouths can be open or closed, and their wings can be in one of three positions, and there's three different styles of eyes.
And it's really cute.
This is actually a pretty good crowd pleaser, usually, especially because sometimes, randomly, they'll be, like, right next to each other and it looks like they're kissing.
Aww.
So I have this style of scroll art I call varying density. The Star Field one was an example of that, where it goes from mostly empty to then completely full and then back to empty.
But I also have this one, which I call Triangle Grid Scaffolding.
So I broke this down—let me go ahead and pause this—I broke this one down into two lines.
So these are always connected. You never just see, like, a single slash by itself, but they're all part of this hidden triangular grid that sort of reveals itself as it becomes more and more fleshed out.
And you can see the entire grid pattern right here.
So you can just take any sort of repeating tessellated pattern like this and apply the varying-density model to it.
I also have another one where I have these striped triangles that are right-side-up triangles and upside-down triangles that also start getting more and more dense, and then using two different types of slashes.
So, all in all, there's four different combinations of these things.
Now, so there's more to text characters than just letters, numbers, and symbols.
Before graphical user interfaces came about, software had text characters that looked like lines, and these were called box-drawing characters.
Usually, if your laundromat is still using some computer from 1985, you might see this as part of their text UI.
It's actually making a bit of a comeback with things like Rich and Textual.
So I call this one Connected Pipes, where I actually completely forget how I programmed this originally.
I think I just wanted to make sure that there were never any lines that were sticking out, right? So I always wanted the text cell to be connected to the one that came before it and above it.
But again, these are just text characters. You can highlight this and copy and paste it into an email. Do whatever you want. It's all just text.
Now, you can also create texture by having light characters going to more filled-in characters.
There was this program that had this ASCII-art rotating donut animation that was pretty popular on YouTube and social media a few years back.
And you can see, like, the period takes up the least amount of space, so it's the lightest or the darkest, depending on whether you have white on black or black on white, whereas the at symbol is mostly filled in.
So I've compiled all of this.
I initially started using this in Python, but I wanted a way to easily show it to other people, so I had to put it on the web. So I created scrollart.org.
I have this sort of text-area terminal JavaScript library that takes a textarea HTML element and gives you a print method for it, essentially, so that you can treat it like a screen to create your own scroll art.
I'm really looking for any contributors there, and I've learned way too much about this weird art form.
I'm not going to go into all of this, but you can download the slides from bit.ly PyBay 2024 scrollart to look at this stuff later.
And I even had some more advanced ideas. I have far more ideas for projects than time to actually work on them. But again, I'll just skip all over that.
But really, these were great, especially for programming projects when I'm finally going to get around to learning Rust or when I'm teaching other people how to code.
And I've put some of these projects into the third edition of Automate the Boring Stuff, which is coming out soon.
I can tell that I'm almost done with the drafts because all the file names are like "Final, final, version 5, final.docx."
But anyway, the best advice that I have for making scroll art—because whenever I tell people, especially software engineers, about this, immediately their mind wants to make it more complicated.
"We can add color. We can add emojis. We can have it running on a refrigerator."
And really, you want to do the complete opposite of that. You want to try to keep all of these programs short, just because it's a lot easier to play around with there.
A lot of the programs I've written in Python, they're usually under about 50 lines of code.
Whenever I have some great idea and I start working on it, and it builds in complexity, and after about three or four hours I realize it's actually not that great of an idea, then I have to throw away all this code.
You know, this isn't really some, like, colorful Processing, p5 sort of thing, or like those demoscene elaborate animations that you see.
You really want to start simple and then just iterate and build up the complexity slowly.
And it's a lot more fun to have several small programs done than try to work on one big, elaborate project that you never actually finish.
And, in fact, my favorite piece that I've created out of all of these is this one, which I call Orbital Travels.
I'll just go ahead and set this to white on black.
So all of this is just sine waves, which is incredibly simple. And I just decide, "Hey, what if I just had six or seven of these going with different characters?"
The name of it comes from: if you look at the orbit of, like, the moon around the Earth, it looks like a circle. But then, when you consider that the Earth is also moving, the movement of the moon is actually a spiral that's going around.
And then you consider that that's moving around the sun, and that's moving around the galactic center.
And, yeah, this is really easy. I can't believe no one else has...
I've looked, and I thought, "Hey, have people done something like this before?" Because it's so mind-bogglingly simple. It's just print and loops.
But I kind of figured, like, "Oh, okay, well, I'll just register a domain name and then fill this out."
Now, this is the part where I still have—I got a double-length slot for this talk, and so I had some remaining time—and this is the part where I can just, like, talk about the scrollart.org website.
I learned a lot of surprising things about this.
Most of these things are completely useless as far as computer science or getting hired in tech. But then again, a lot of science and commercial applications began with silly, useless experiments.
I won't go into too many of these, but one of these was in the music video.
And I accidentally discovered you can do parallax with scroll art.
So that's sort of like when you're looking out your car window and the things near you are moving faster than the mountains in the distance. They appear to be moving slower.
In this, we have the skulls in the background that seem to be moving slower than the hearts in the foreground that are moving up a lot faster, which seems like it's impossible.
Because if we stop this, this is just text. It's just a static piece of text.
And I discovered this completely by accident.
So these skulls, I think, are about eight rows long. And so, you know, if you actually—if you just print one row at a time, it looks like they're just moving up one row at a time.
But if you print nine rows at a time, because they're a repeating pattern, they're actually jumping up an entire pattern and then moving one row up.
So it sort of looks like they're moving a lot slower. They're only moving one row at a time.
But meanwhile, the hearts, which are not repeating, those you can see: "Oh, those are moving up nine rows at a time."
And I realized, like, wow, you could just have as many layers of this as you want to create all sorts of weird animated effects like that.
So anyway, I hope you found the idea of scroll art as intriguing as I have, and I hope that you decide to share your own scroll art programs.
You know, I love looking at the output of these programs and then just thinking about how the code behind them works.
And that's my favorite kind of art.
I love going to art museums and looking at things that give me ideas for creations of my own—my own that I want to make.
So, as said in the Zen of Python, beautiful is better than ugly, and simple is better than complex.
And you can get a lot of beauty from simplicity.
Thank you very much.
So, yeah, I'll be out doing the book signing, or you can also purchase books, or you can just talk to me if you have any questions about scroll art.
And thank you very much.