-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathrun_html_render.py
More file actions
232 lines (152 loc) · 5.77 KB
/
run_html_render.py
File metadata and controls
232 lines (152 loc) · 5.77 KB
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
#!/usr/bin/env python
"""
B3 copy
a simple script can run and test your html rendering classes.
Uncomment the steps as you add to your rendering.
"""
from io import StringIO
# importing the html_rendering code with a short name for easy typing.
import html_render as hr
# reloading in case you are running this in iPython
# -- we want to make sure the latest version is used
import importlib
importlib.reload(hr)
# writing the file out:
def render_page(page, filename):
"""
render the tree of elements
This uses StringIO to render to memory, then dump to console and
write to file -- very handy!
"""
f = StringIO() #b3 - here f is the string going into **memory (object f)
page.render(f, " ") # b3 - here is where we take object: "page" and execute func render. the f attribute is the output file in func render = so f is a memory locatoin (defined by StringIO) and writes to object: f in memory
f.seek(0) #b3 - go back to start of f
print("\nthis is f.read from func render_page:\n ",f.read()) #b3 - here it dumps to **console by reading f (from memory)
f.seek(0)
open(filename, 'w').write(f.read()) #here is where it writes to a file - object f is read from memory and writen to the output file name.html given above
# Step 1
#########
'''
page = hr.Element() #B3 - this starts an object using the Element class in file: html_render = page becomes a member of class Element
page.append("Here is a paragraph of text -- there could be more of them, but this is enough to show that we can do some text. \n")
page.append("\nAnd here is another piece of text -- you should be able to add any number - like 423\n")
print("b3test page: ", type(page), "page dot append is: ", page.append, "\n\n")
render_page(page, "test_html_output1.html") # B3- this is a function in this file: see above. it says print object: "page" to file: "in the quotes"
'''
## Step 2
# ##########
'''
page = hr.Html()
body = hr.Body()
body.append(hr.P("Here is a paragraph of text -- there could be more of them, but this is enough to show that we can do some text"))
body.append(hr.P("And here is another piece of text -- you should be able to add any number"))
page.append(body)
render_page(page, "test_html_output2.html")
'''
# # Step 3
# ##########
'''
page = hr.Html()
head = hr.Head()
head.append(hr.Title("PythonClass = Revision 1087:"))
page.append(head)
body = hr.Body()
body.append(hr.P("Here is a paragraph of text -- there could be more of them, but this is enough to show that we can do some text"))
body.append(hr.P("And here is another piece of text -- you should be able to add any number"))
page.append(body)
render_page(page, "test_html_output3.html")
'''
# # Step 4
# ##########
'''
page = hr.Html()
head = hr.Head()
head.append(hr.Title("PythonClass = Revision 1087:"))
page.append(head)
body = hr.Body()
body.append(hr.P("Here is a paragraph of text -- there could be more of them, but this is enough to show that we can do some text",
style="text-align: center; font-style: oblique;"))
page.append(body)
render_page(page, "test_html_output4.html")
'''
# # Step 5
# #########
'''
page = hr.Html()
head = hr.Head()
head.append(hr.Title("PythonClass = Revision 1087:"))
page.append(head)
body = hr.Body()
body.append(hr.P("Here is a paragraph of text -- there could be more of them, but this is enough to show that we can do some text",
style="text-align: center; font-style: oblique;"))
body.append(hr.Hr())
page.append(body)
render_page(page, "test_html_output5.html")
'''
# # Step 6
# #########
'''
page = hr.Html()
head = hr.Head()
head.append(hr.Title("PythonClass = Revision 1087:"))
page.append(head)
body = hr.Body()
body.append(hr.P("Here is a paragraph of text -- there could be more of them, but this is enough to show that we can do some text",
style="text-align: center; font-style: oblique;"))
body.append(hr.Hr())
body.append("And this is a ")
body.append( hr.A("http://google.com", "link") )
body.append("to google")
page.append(body)
render_page(page, "test_html_output6.html")
'''
# # Step 7
# #########
'''
page = hr.Html()
head = hr.Head()
head.append(hr.Title("PythonClass = Revision 1087:"))
page.append(head)
body = hr.Body()
body.append( hr.H(2, "PythonClass - Class 6 example") )
body.append(hr.P("Here is a paragraph of text -- there could be more of them, but this is enough to show that we can do some text",
style="text-align: center; font-style: oblique;"))
body.append(hr.Hr())
list = hr.Ul(id="TheList", style="line-height:200%")
list.append( hr.Li("The first item in a list") )
list.append( hr.Li("This is the second item", style="color: red") )
item = hr.Li()
item.append("And this is a ")
item.append( hr.A("http://google.com", "link") )
item.append("to google")
list.append(item)
body.append(list)
page.append(body)
render_page(page, "test_html_output7a.html")
# # Step 8
# ########
'''
page = hr.Html()
#make and build the head
head = hr.Head()
head.append( hr.Meta(charset="UTF-8") )
head.append(hr.Title("PythonClass = Revision 1087:"))
#tuck head into the page
page.append(head)
#create and build the body
body = hr.Body()
body.append( hr.H(2, "PythonClass - Class 6 example") )
body.append(hr.P("Here is a paragraph of text -- there could be more of them, but this is enough to show that we can do some text",
style="text-align: center; font-style: oblique;"))
body.append(hr.Hr())
list = hr.Ul(id="TheList", style="line-height:200%")
list.append( hr.Li("The first item in a list") )
list.append( hr.Li("This is the second item", style="color: red") )
item = hr.Li()
item.append("And this is a ")
item.append( hr.A("http://google.com", "link") )
item.append("to google")
list.append(item)
body.append(list)
page.append(body)
render_page(page, "test_html_output8.html")