Beginning Python - From Novice to Professional
Beginning Python - From Novice to Professional Beginning Python - From Novice to Professional
CHAPTER 21 ■ ■ ■ Project 2: Painting a Pretty Picture In this project, you learn how you can create graphics in Python. More specifically, you create a PDF file with graphics helping you visualize data that you read from a text file. While you could get such functionality from a regular spreadsheet, Python gives you much more power, as you’ll see when you get to the second implementation and automatically download your data from the Internet. ALPHABET SOUP REVISITED In the previous chapter, we looked at HTML and XML—and here is another acronym: PDF, short for Portable Document Format. PDF is a format created by Adobe that can represent any kind of document with graphics and text. The PDF file is not editable (as, say, a Microsoft Word file would be), but there is reader software freely available for most platforms, and the PDF file should look the same no matter which reader you use or which platform you are on (as opposed to HTML, with which the correct fonts may not be available, you’d have to ship pictures as separate files, and so on). If you don’t already have a PDF reader, Adobe’s own Acrobat Reader is freely available from the Adobe Web site (http://adobe.com/products/acrobat/readstep.html). What’s the Problem? Python is excellent for analyzing data. With its file handling and string processing facilities, it’s probably easier to create some form of report from a data file than to create something similar in your average spreadsheet, especially if what you want to do requires some complicated programming logic. You have seen (in Chapter 3) how you can use string formatting to get pretty output—for example, if you want to print numbers in columns. However, sometimes plain text just isn’t enough. (As they say, a picture is worth a thousand words.) In this project, you learn the basics of the ReportLab package, which enables you to create graphics and documents in the PDF format (and a few other formats) almost as easily as you created plain text earlier. As you play with the concepts in this project, I encourage you to find some application that is interesting to you. I have chosen to use data about sunspots (from the Space Environment 411
412 CHAPTER 21 ■ PROJECT 2: PAINTING A PRETTY PICTURE Center, a part of the U.S. National Oceanic and Atmospheric Administration) and to create a line diagram from this data. Specific Goals The program should be able to do the following: • Download a data file from the Internet. • Parse the data file and extract the interesting parts. • Create PDF graphics based on the data. As in the previous project, these goals might not be fully met by the first prototype. Useful Tools The crucial tool in this project is the graphics-generating package. There are quite a few such packages to choose from. If you visit the Vaults of Parnassus site (http://www.vex.net/parnassus), you will find a separate category for graphics. I have chosen to use ReportLab because it is easy to use and has extensive functionality for both graphics and document generation in PDF. If you want to go beyond the basics, you might also want to consider the P Y X graphics package (http://pyx.sf.net), which is really powerful, and has support for TEX -based typography. To get the ReportLab package, go to the official Web page at http://reportlab.org. There you will find the software, documentation, and samples. The software should be available at http://reportlab.org/downloads.html. Simply download the ReportLab toolkit, uncompress the archive (ReportLab_X.zip, where X is a version number), and put the resulting directory in your Python path. When you have done this, you should be able to import the reportlab module, as follows: >>> import reportlab >>> How Does It Work? Although I show you how some ReportLab features work in this project, much more functionality is available. To learn more, I suggest you obtain the user guide and the (separate) graphics guide, made available on the ReportLab Web site (on the documentation page). They are quite readable, and are much more comprehensive than this one chapter could possibly be. Preparations Before we start programming, we need some data with which to test our program. I have chosen (quite arbitrarily) to use data about sunspots, available from the Web site of the Space Environment Center (http://www.sec.noaa.gov). You can find the data I use in my examples at http:// www.sec.noaa.gov/ftpdir/weekly/Predict.txt. This data file is updated weekly and contains information about sunspots and radio flux. (Don’t ask me what that means.) Once you’ve got this file, you’re ready to start playing with the problem.
- Page 391 and 392: 360 CHAPTER 17 ■ EXTENDING PYTHON
- Page 393 and 394: 362 CHAPTER 17 ■ EXTENDING PYTHON
- Page 395 and 396: 364 CHAPTER 17 ■ EXTENDING PYTHON
- Page 397 and 398: 366 CHAPTER 17 ■ EXTENDING PYTHON
- Page 399 and 400: 368 CHAPTER 17 ■ EXTENDING PYTHON
- Page 401 and 402: 370 CHAPTER 17 ■ EXTENDING PYTHON
- Page 404 and 405: CHAPTER 18 ■ ■ ■ Packaging Yo
- Page 406 and 407: CHAPTER 18 ■ PACKAGING YOUR PROGR
- Page 408 and 409: CHAPTER 18 ■ PACKAGING YOUR PROGR
- Page 410 and 411: CHAPTER 18 ■ PACKAGING YOUR PROGR
- Page 412 and 413: CHAPTER 19 ■ ■ ■ Playful Prog
- Page 414 and 415: CHAPTER 19 ■ PLAYFUL PROGRAMMING
- Page 416 and 417: CHAPTER 19 ■ PLAYFUL PROGRAMMING
- Page 418 and 419: CHAPTER 19 ■ PLAYFUL PROGRAMMING
- Page 420: CHAPTER 19 ■ PLAYFUL PROGRAMMING
- Page 423 and 424: 392 CHAPTER 20 ■ PROJECT 1: INSTA
- Page 425 and 426: 394 CHAPTER 20 ■ PROJECT 1: INSTA
- Page 427 and 428: 396 CHAPTER 20 ■ PROJECT 1: INSTA
- Page 429 and 430: 398 CHAPTER 20 ■ PROJECT 1: INSTA
- Page 431 and 432: 400 CHAPTER 20 ■ PROJECT 1: INSTA
- Page 433 and 434: 402 CHAPTER 20 ■ PROJECT 1: INSTA
- Page 435 and 436: 404 CHAPTER 20 ■ PROJECT 1: INSTA
- Page 437 and 438: 406 CHAPTER 20 ■ PROJECT 1: INSTA
- Page 439 and 440: 408 CHAPTER 20 ■ PROJECT 1: INSTA
- Page 444 and 445: CHAPTER 21 ■ PROJECT 2: PAINTING
- Page 446 and 447: CHAPTER 21 ■ PROJECT 2: PAINTING
- Page 448 and 449: CHAPTER 21 ■ PROJECT 2: PAINTING
- Page 450 and 451: CHAPTER 21 ■ PROJECT 2: PAINTING
- Page 452 and 453: CHAPTER 22 ■ ■ ■ Project 3: X
- Page 454 and 455: CHAPTER 22 ■ PROJECT 3: XML FOR A
- Page 456 and 457: CHAPTER 22 ■ PROJECT 3: XML FOR A
- Page 458 and 459: CHAPTER 22 ■ PROJECT 3: XML FOR A
- Page 460 and 461: CHAPTER 22 ■ PROJECT 3: XML FOR A
- Page 462 and 463: CHAPTER 22 ■ PROJECT 3: XML FOR A
- Page 464 and 465: CHAPTER 22 ■ PROJECT 3: XML FOR A
- Page 466 and 467: CHAPTER 22 ■ PROJECT 3: XML FOR A
- Page 468: CHAPTER 22 ■ PROJECT 3: XML FOR A
- Page 471 and 472: 440 CHAPTER 23 ■ PROJECT 4: IN TH
- Page 473 and 474: 442 CHAPTER 23 ■ PROJECT 4: IN TH
- Page 475 and 476: 444 CHAPTER 23 ■ PROJECT 4: IN TH
- Page 477 and 478: 446 CHAPTER 23 ■ PROJECT 4: IN TH
- Page 479 and 480: 448 CHAPTER 23 ■ PROJECT 4: IN TH
- Page 481 and 482: 450 CHAPTER 23 ■ PROJECT 4: IN TH
- Page 483 and 484: 452 CHAPTER 23 ■ PROJECT 4: IN TH
- Page 486 and 487: CHAPTER 24 ■ ■ ■ Project 5: A
- Page 488 and 489: CHAPTER 24 ■ PROJECT 5: A VIRTUAL
- Page 490 and 491: CHAPTER 24 ■ PROJECT 5: A VIRTUAL
412 CHAPTER 21 ■ PROJECT 2: PAINTING A PRETTY PICTURE<br />
Center, a part of the U.S. National Oceanic and Atmospheric Administration) and <strong>to</strong> create a<br />
line diagram from this data.<br />
Specific Goals<br />
The program should be able <strong>to</strong> do the following:<br />
• Download a data file from the Internet.<br />
• Parse the data file and extract the interesting parts.<br />
• Create PDF graphics based on the data.<br />
As in the previous project, these goals might not be fully met by the first pro<strong>to</strong>type.<br />
Useful Tools<br />
The crucial <strong>to</strong>ol in this project is the graphics-generating package. There are quite a few such<br />
packages <strong>to</strong> choose from. If you visit the Vaults of Parnassus site (http://www.vex.net/parnassus),<br />
you will find a separate category for graphics. I have chosen <strong>to</strong> use ReportLab because it is easy<br />
<strong>to</strong> use and has extensive functionality for both graphics and document generation in PDF. If<br />
you want <strong>to</strong> go beyond the basics, you might also want <strong>to</strong> consider the P Y X graphics package<br />
(http://pyx.sf.net), which is really powerful, and has support for TEX -based typography.<br />
To get the ReportLab package, go <strong>to</strong> the official Web page at http://reportlab.org. There<br />
you will find the software, documentation, and samples. The software should be available at<br />
http://reportlab.org/downloads.html. Simply download the ReportLab <strong>to</strong>olkit, uncompress<br />
the archive (ReportLab_X.zip, where X is a version number), and put the resulting direc<strong>to</strong>ry in<br />
your <strong>Python</strong> path.<br />
When you have done this, you should be able <strong>to</strong> import the reportlab module, as follows:<br />
>>> import reportlab<br />
>>><br />
How Does It Work?<br />
Although I show you how some ReportLab features work in this project, much more functionality<br />
is available. To learn more, I suggest you obtain the user guide and the (separate) graphics<br />
guide, made available on the ReportLab Web site (on the documentation page). They are quite<br />
readable, and are much more comprehensive than this one chapter could possibly be.<br />
Preparations<br />
Before we start programming, we need some data with which <strong>to</strong> test our program. I have chosen<br />
(quite arbitrarily) <strong>to</strong> use data about sunspots, available from the Web site of the Space Environment<br />
Center (http://www.sec.noaa.gov). You can find the data I use in my examples at http://<br />
www.sec.noaa.gov/ftpdir/weekly/Predict.txt.<br />
This data file is updated weekly and contains information about sunspots and radio flux.<br />
(Don’t ask me what that means.) Once you’ve got this file, you’re ready <strong>to</strong> start playing with<br />
the problem.