@@ -42,18 +42,18 @@ def __str__(self) -> str:
42
42
class Book :
43
43
"""Class used for example data"""
44
44
45
- def __init__ (self , title : str , due_date : str ) -> None :
45
+ def __init__ (self , title : str , year_published : str ) -> None :
46
46
self .title = title
47
- self .due_date = due_date
47
+ self .year_published = year_published
48
48
49
49
50
- class Person :
50
+ class Author :
51
51
"""Class used for example data"""
52
52
53
- def __init__ (self , name : str , birthday : str , department : str ) -> None :
53
+ def __init__ (self , name : str , birthday : str , place_of_birth : str ) -> None :
54
54
self .name = name
55
55
self .birthday = birthday
56
- self .department = department
56
+ self .place_of_birth = place_of_birth
57
57
self .books : List [Book ] = []
58
58
59
59
@@ -108,48 +108,61 @@ def basic_tables():
108
108
109
109
def nested_tables ():
110
110
"""
111
- Demonstrates how to nest tables using the override_data_style keyword to handle tables with conflicting styles.
111
+ Demonstrates how to nest tables using the style_data_text keyword to handle tables with conflicting styles.
112
112
In these cases, the inner tables reset the background color applied by the outer AlternatingTable.
113
113
114
114
It also demonstrates coloring various aspects of tables.
115
115
"""
116
116
117
117
# Create data for this example
118
- person_data : List [Person ] = []
119
- person_1 = Person ("Bill Anderson" , "01/22/1955" , "Accounting" )
120
- person_1 .books .append (Book ("Great Expectations" , "11/01/2025" ))
121
- person_1 .books .append (Book ("Strange Case of Dr Jekyll and Mr Hyde" , "07/16/2026" ))
122
- person_1 .books .append (Book ("Dune" , "01/24/2027" ))
123
-
124
- person_2 = Person ("Arthur Smith" , "06/11/1974" , "Automotive" )
125
- person_2 .books .append (Book ("Nineteen Eighty-Four" , "08/07/2025" ))
126
- person_2 .books .append (Book ("Pride and Prejudice" , "04/13/2026" ))
127
- person_2 .books .append (Book ("Fahrenheit 451" , "07/29/2026" ))
128
- person_2 .books .append (Book ("The Count of Monte Cristo" , "10/15/2027" ))
129
-
130
- person_data .append (person_1 )
131
- person_data .append (person_2 )
132
-
133
- # Define table which presents Person data fields vertically with no header.
118
+ author_data : List [Author ] = []
119
+ author_1 = Author ("Frank Herbert" , "10/08/1920" , "Tacoma, Washington" )
120
+ author_1 .books .append (Book ("Dune" , "1965" ))
121
+ author_1 .books .append (Book ("Dune Messiah" , "1969" ))
122
+ author_1 .books .append (Book ("Children of Dune" , "1976" ))
123
+ author_1 .books .append (Book ("God Emperor of Dune" , "1981" ))
124
+ author_1 .books .append (Book ("Heretics of Dune" , "1984" ))
125
+ author_1 .books .append (Book ("Chapterhouse: Dune" , "1985" ))
126
+
127
+ author_2 = Author ("Jane Austen" , "12/16/1775" , "Steventon, Hampshire, England" )
128
+ author_2 .books .append (Book ("Sense and Sensibility" , "1811" ))
129
+ author_2 .books .append (Book ("Pride and Prejudice" , "1813" ))
130
+ author_2 .books .append (Book ("Mansfield Park " , "1814" ))
131
+ author_2 .books .append (Book ("Emma" , "1815" ))
132
+ author_2 .books .append (Book ("Northanger Abbey" , "1818" ))
133
+ author_2 .books .append (Book ("Persuasion" , "1818" ))
134
+ author_2 .books .append (Book ("Lady Susan" , "1871" ))
135
+
136
+ author_data .append (author_1 )
137
+ author_data .append (author_2 )
138
+
139
+ # Define table which presents Author data fields vertically with no header.
134
140
# This will be nested in the parent table.
135
- person_columns : List [Column ] = list ()
136
- person_columns .append (Column ("" , width = 10 ))
137
- person_columns .append (Column ("" , width = 20 ))
141
+ author_columns : List [Column ] = list ()
142
+ author_columns .append (Column ("" , width = 14 ))
143
+ author_columns .append (Column ("" , width = 20 ))
138
144
139
145
# The text labels in this table will be bold text. They will also be aligned by the table code.
140
146
# When styled text is aligned, a TextStyle.RESET_ALL sequence is inserted between the aligned text
141
- # and the fill characters. Therefore, the Person table will contain TextStyle.RESET_ALL sequences,
147
+ # and the fill characters. Therefore, the Author table will contain TextStyle.RESET_ALL sequences,
142
148
# which would interfere with the background color applied by the parent table. To account for this,
143
- # we will color the Person tables to match the background colors of the parent AlternatingTable's rows
144
- # and set override_data_style to False in the Person column. See below for that.
145
- odd_person_tbl = SimpleTable (person_columns , data_bg = EightBitBg .GRAY_0 )
146
- even_person_tbl = SimpleTable (person_columns , data_bg = EightBitBg .GRAY_15 )
149
+ # we will color the Author tables to match the background colors of the parent AlternatingTable's rows
150
+ # and set style_data_text to False in the Author column. See below for that.
151
+ odd_author_tbl = SimpleTable (author_columns , data_bg = EightBitBg .GRAY_0 )
152
+ even_author_tbl = SimpleTable (author_columns , data_bg = EightBitBg .GRAY_15 )
147
153
148
154
# Define AlternatingTable table for books checked out by people in the first table.
149
155
# This will also be nested in the parent table.
150
156
books_columns : List [Column ] = list ()
151
- books_columns .append (Column ("Title" , width = 28 ))
152
- books_columns .append (Column ("Due Date" , width = 10 ))
157
+ books_columns .append (Column ("Title" , width = 25 ))
158
+ books_columns .append (
159
+ Column (
160
+ "Published" ,
161
+ width = 9 ,
162
+ header_horiz_align = HorizontalAlignment .RIGHT ,
163
+ data_horiz_align = HorizontalAlignment .RIGHT ,
164
+ )
165
+ )
153
166
154
167
books_tbl = AlternatingTable (
155
168
books_columns ,
@@ -160,13 +173,13 @@ def nested_tables():
160
173
even_bg = EightBitBg .GRAY_15 ,
161
174
)
162
175
163
- # Define parent AlternatingTable which contains Person and Book tables
176
+ # Define parent AlternatingTable which contains Author and Book tables
164
177
parent_tbl_columns : List [Column ] = list ()
165
178
166
- # Both the Person and Books tables already have background colors. Set override_data_style
179
+ # Both the Author and Books tables already have background colors. Set style_data_text
167
180
# to False so the parent AlternatingTable does not apply background color to them.
168
- parent_tbl_columns .append (Column ("Person " , width = odd_person_tbl .total_width (), override_data_style = False ))
169
- parent_tbl_columns .append (Column ("Books" , width = books_tbl .total_width (), override_data_style = False ))
181
+ parent_tbl_columns .append (Column ("Author " , width = odd_author_tbl .total_width (), style_data_text = False ))
182
+ parent_tbl_columns .append (Column ("Books" , width = books_tbl .total_width (), style_data_text = False ))
170
183
171
184
parent_tbl = AlternatingTable (
172
185
parent_tbl_columns ,
@@ -178,26 +191,26 @@ def nested_tables():
178
191
179
192
# Construct the tables
180
193
parent_table_data : List [List [Any ]] = []
181
- for row , person in enumerate (person_data , start = 1 ):
182
- # First build the person table and color it based on row number
183
- person_tbl = even_person_tbl if row % 2 == 0 else odd_person_tbl
194
+ for row , author in enumerate (author_data , start = 1 ):
195
+ # First build the author table and color it based on row number
196
+ author_tbl = even_author_tbl if row % 2 == 0 else odd_author_tbl
184
197
185
198
# This table has three rows and two columns
186
199
table_data = [
187
- [ansi .style ("Name" , bold = True ), person .name ],
188
- [ansi .style ("Birthday" , bold = True ), person .birthday ],
189
- [ansi .style ("Department " , bold = True ), person . department ],
200
+ [ansi .style ("Name" , bold = True ), author .name ],
201
+ [ansi .style ("Birthday" , bold = True ), author .birthday ],
202
+ [ansi .style ("Place of Birth " , bold = True ), author . place_of_birth ],
190
203
]
191
204
192
- # Build the person table string
193
- person_tbl_str = person_tbl .generate_table (table_data , include_header = False , row_spacing = 0 )
205
+ # Build the author table string
206
+ author_tbl_str = author_tbl .generate_table (table_data , include_header = False , row_spacing = 0 )
194
207
195
- # Now build this person 's book table
196
- table_data = [[book .title , book .due_date ] for book in person .books ]
208
+ # Now build this author 's book table
209
+ table_data = [[book .title , book .year_published ] for book in author .books ]
197
210
book_tbl_str = books_tbl .generate_table (table_data )
198
211
199
212
# Add these tables to the parent table's data
200
- parent_table_data .append (['\n ' + person_tbl_str , '\n ' + book_tbl_str + '\n \n ' ])
213
+ parent_table_data .append (['\n ' + author_tbl_str , '\n ' + book_tbl_str + '\n \n ' ])
201
214
202
215
# Build the parent table
203
216
top_table_str = parent_tbl .generate_table (parent_table_data )
0 commit comments