Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Circle Class and other updates #204

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
63 changes: 63 additions & 0 deletions students/ericstreit/session07/html_render.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#!/usr/bin/env python3
####stopped at step 4
"""
A class-based system for rendering html.
"""


# This is the framework for the base class
class Element(object):

tag = "html"

def __init__(self, content=None, **kwargs):
self.contents = [content]
# print("contents is:", self.contents)

def append(self, new_content):
self.contents.append(new_content)

def render(self, out_file):
# Loop through the list of contents:
out_file.write("<{}>\n".format(self.tag))
for content in self.contents:
if content is not None:
try:
content.render(out_file)
except AttributeError:
out_file.write(content)
out_file.write("\n")
out_file.write("</{}>\n".format(self.tag))

class Html(Element):

tag = "html"

class Body(Element):

tag = "body"

class P(Element):

tag = "p"

class Head(Element):

tag = "head"

class OneLineTag(Element):

def render(self, out_file):
# One line render:
#removing my way and implementing the tutorial way
#out_file.write("<{}> {} </{}>\n".format(self.tag, self.contents, self.tag))
out_file.write("<{}>".format(self.tag))
out_file.write(self.contents[0])
out_file.write("</{}>\n".format(self.tag))

def append(self, content):
raise NotImplementedError

class Title(OneLineTag):

tag = "title"
231 changes: 231 additions & 0 deletions students/ericstreit/session07/run_html_render.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,231 @@
#!/usr/bin/env python3

"""
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


# writing the file out:
def render_page(page, filename, indent=None):
"""
render the tree of elements

This uses StringIO to render to memory, then dump to console and
write to file -- very handy!
"""

f = StringIO()
if indent is None:
page.render(f)
else:
page.render(f, indent)

print(f.getvalue())
with open(filename, 'w') as outfile:
outfile.write(f.getvalue())


# Step 1
#########

page = hr.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")

page.append("And here is another piece of text -- you should be able to add any number")

render_page(page, "test_html_output1.html")

# The rest of the steps have been commented out.
# Uncomment them as you move along with the assignment.

# ## 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_output7.html")

# # Step 8 and 9
# ##############

# page = hr.Html()


# head = hr.Head()
# head.append( hr.Meta(charset="UTF-8") )
# head.append(hr.Title("PythonClass = Revision 1087:"))

# page.append(head)

# body = hr.Body()

# body.append( hr.H(2, "PythonClass - 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")
27 changes: 27 additions & 0 deletions students/ericstreit/session07/sample_html.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>Python Class Sample page</title>
</head>
<body>
<h2>Python Class - Html rendering example</h2>
<p style="text-align: center; font-style: oblique;">
Here is a paragraph of text -- there could be more of them, but this is enough to show that we can do some text
</p>
<hr />
<ul id="TheList" style="line-height:200%">
<li>
The first item in a list
</li>
<li style="color: red">
This is the second item
</li>
<li>
And this is a
<a href="http://google.com">link</a>
to google
</li>
</ul>
</body>
</html>
Loading