Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
First html render commit.
  • Loading branch information
Michael Gregor
Michael Gregor committed Feb 22, 2016
commit 03c630c2949a975af3d934903773423c4ae5c75b
30 changes: 30 additions & 0 deletions students/MichaelGregor/Session 7/html_render.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#!/usr/bin/env python

"""
Python class example.
"""


# The start of it all:
# Fill it all in here.
class Element(object):

def __init__(self, content=None):
self.content = '<!DOCTYPE html>\n'

def append(self, new_content):
self.content += new_content

def render(self, file_out, ind=""):
file_out.write(self.content)

class Html(Element):

def __init__(self, content=None):
self.content = '\t<html>\n'


class Body(Element):

def __init__(self, content=None):
self.content = '\t\t<body>\n'
224 changes: 224 additions & 0 deletions students/MichaelGregor/Session 7/run_html_render.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,224 @@
#!/usr/bin/env python

"""
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()
page.render(f, " ")

f.seek(0)

print(f.read())

f.seek(0)
open(filename, 'w').write(f.read())


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

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

# 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 - 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")
27 changes: 27 additions & 0 deletions students/MichaelGregor/Session 7/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>PythonClass = Revision 1087:</title>
</head>
<body>
<h2>PythonClass - Class 6 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 style="line-height:200%" id="TheList">
<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>
2 changes: 2 additions & 0 deletions students/MichaelGregor/Session 7/test_html_output1.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<!DOCTYPE html>
Here is a paragraph of text -- there could be more of them, but this is enough to show that we can do some textAnd here is another piece of text -- you should be able to add any number