From 3299d60120556933e08981a77cfe91672e78a0c6 Mon Sep 17 00:00:00 2001 From: krishna Bindhu Date: Thu, 10 Mar 2016 14:39:02 -0800 Subject: [PATCH] HTML EXCERCISE --- Examples/Session07/html_render/html_render.py | 58 ++++- .../Session07/html_render/run_html_render.py | 15 +- .../html_render/test_html_output1.html | 8 +- students/kbindhu/session08/html_render.py | 136 +++++++++++ students/kbindhu/session08/run_html_render.py | 220 ++++++++++++++++++ .../kbindhu/session08/test_html_output8.html | 31 +++ 6 files changed, 447 insertions(+), 21 deletions(-) create mode 100644 students/kbindhu/session08/html_render.py create mode 100644 students/kbindhu/session08/run_html_render.py create mode 100644 students/kbindhu/session08/test_html_output8.html diff --git a/Examples/Session07/html_render/html_render.py b/Examples/Session07/html_render/html_render.py index f46760f..c7beab4 100755 --- a/Examples/Session07/html_render/html_render.py +++ b/Examples/Session07/html_render/html_render.py @@ -1,17 +1,59 @@ #!/usr/bin/env python - +#Krishna Bindhu +#Date:3/2/2016 """ Python class example. """ - +#calss ELEMENT # The start of it all: # Fill it all in here. class Element(object): + """class attributes""" + tag ='' + indent='' + """class methods""" + def __init__(self, content=""): + self.content=[] + if content is not None: + self.content.append(content) + def append(self, content): + self.content.append(content) + def render(self, file_out,ind=''): + file_out.write(ind*4+self.tag) + file_out.write('\n') + file_out.write(str(self.content)) + file_out.write('\n') + file_out.write(ind*4+"<\"+self.tag+">'') + +""" +#subclass html +class Html(Element): + tag='' + def __init__(self, content=""): + super().__init__(content) - def __init__(self, content=None): - pass - def append(self, new_content): - pass - def render(self, file_out, ind=""): - file_out.write("just something as a place holder...") + + +class Body(Element): + tag='' + def __init__(self, content=""): + super().__init__(content) + def render(self, file_out,ind=''): + file_out.write(ind*8+self.tag+"\n") + file_out.write(self.content) + file_out.write('\n') + file_out.write(ind*8+"<\body>") + +class P(Element): + tag='

' + def __init__(self, content=""): + super().__init__(content) + def append(self, content): + super(P,self).append(content) + def render(self, file_out,ind=''): + file_out.write(ind*12+self.tag+"\n") + file_out.write(ind*16+self.content) + file_out.write('\n') + file_out.write(ind*8+"<\p>") +""" diff --git a/Examples/Session07/html_render/run_html_render.py b/Examples/Session07/html_render/run_html_render.py index 663e5ff..01b9d88 100644 --- a/Examples/Session07/html_render/run_html_render.py +++ b/Examples/Session07/html_render/run_html_render.py @@ -46,22 +46,21 @@ def render_page(page, filename): page.append("And here is another piece of text -- you should be able to add any number") -render_page(page, "test_html_output1.html") +render_page(page, "test_html_output1_kr.html") # ## Step 2 # ########## -# page = hr.Html() - -# body = hr.Body() +#page = hr.Html() -# 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 = hr.Body() -# body.append(hr.P("And here is another piece of text -- you should be able to add any number")) +#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")) -# page.append(body) +#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") +#render_page(page, "test_html_output2_krhtml") # # Step 3 # ########## diff --git a/Examples/Session07/html_render/test_html_output1.html b/Examples/Session07/html_render/test_html_output1.html index 65c2d86..1043db0 100644 --- a/Examples/Session07/html_render/test_html_output1.html +++ b/Examples/Session07/html_render/test_html_output1.html @@ -1,5 +1,3 @@ - - - Here is a paragraph of text -- there could be more of them, but this is enough to show that we can do some text - And here is another piece of text -- you should be able to add any number - \ No newline at end of file + +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 + <\html> \ No newline at end of file diff --git a/students/kbindhu/session08/html_render.py b/students/kbindhu/session08/html_render.py new file mode 100644 index 0000000..ab79d68 --- /dev/null +++ b/students/kbindhu/session08/html_render.py @@ -0,0 +1,136 @@ +#!/usr/bin/env python +#Krishna Bindhu +#Date:3/2/2016 +""" +Python class example. +""" + +#calss ELEMENT +# The start of it all: +# Fill it all in here. + +class Element(object): + """class attributes""" + tag ='html' + """class methods""" + def __init__(self, content1=None,**kwargs): + #if nothing passed content is an empty list + self.content=[] + self.kwargs=kwargs + if content1 is not None: + #appending strings/objects passed to list + self.content.append(content1) + def append(self, content1): + self.content.append(content1) + def render(self, file_out,ind=' '): + start_tag="<{}>".format(self.tag) + # this for step4 adding additional arguments for tags + for k,v in self.kwargs.items(): + start_tag="<{} {}={}>".format(self.tag,k,v) + file_out.write(ind+start_tag) + file_out.write('\n') + # iterating through the list + for i in range(0,len(self.content)): + #checking the member in a list is a string + #if string add 4 spaces and string + if (isinstance(self.content[i], str)): + file_out.write(ind + ' ' + self.content[i]) + file_out.write('\n') + else: + # call recursively the render if the content appended is a class and add 4 spaces + self.content[i].render(file_out,ind + ' ') + file_out.write('\n') + end_tag="".format(self.tag) + file_out.write(ind+end_tag) + + +#subclass HTML + +class Html(Element): + #overiding tag + tag='html' + def render(self, file_out,ind=' '): + file_out.write("\n") + super().render(file_out,ind=" ") +#subclass HTML +class Body(Element): + #overiding tag + tag='body' + +class P(Element): + #overiding tag + tag='p' +#subclass Head +class Head(Element): + #overiding tag + tag='head' +#One lineTag subclass +class OneLineTag(Element): + #overiding the render method to print content in one line + def render(self, file_out,ind=' '): + start_tag="<{}>".format(self.tag) + file_out.write(ind+start_tag) + # iterating through the list + for i in range(0,len(self.content)): + #checking the member in a list is a string + if (isinstance(self.content[i], str)): + file_out.write(self.content[i]) + else: + # call recursively the render if the content appended is a class + self.content[i].render(file_out,ind) + end_tag="".format(self.tag) + file_out.write(end_tag) + file_out.write('\n') + +#subclass Title +class Title(OneLineTag): + tag='title' + + #SelfClosing Tag subclass +class SelfClosingTag(Element): + def __init__(self, content=None, **kwargs): + super().__init__(content, **kwargs) +#overirding render method to render end tag alone + def render(self, file_out,ind=' '): + #checking whether arguments are empty(this is done for self closing tags br and hr where there are no arguments) + if(self.kwargs is None): + end_tag="<{} />".format(self.tag) + file_out.write(ind+end_tag) + #this is for meta class wnen there ia argument being passed + else: + for k,v in self.kwargs.items(): + end_tag="<{} {}= {}/>".format(self.tag,k,v) + file_out.write(ind+end_tag) +#subclass Hr and Br +class Hr(SelfClosingTag): + tag='hr' +class Hr(SelfClosingTag): + tag='br' + +#Anchor class +class A(Element): + tag='a' + def __init__(self, content,link): + super().__init__(content) + #passing link as keywrod argument to element class + self.kwargs["href"]=link + +#subclass Unordered List +class Ul(Element): + tag="ul" + +#ordered List +class Li(Element): + tag="li" +#subclass H +class H(OneLineTag): + def __init__(self,number,content): + self.tag="h"+str(number) + super().__init__(content) +#subclass Meta +class Meta(SelfClosingTag): + tag='meta' + def __init__(self,content=None,**kwargs): + super().__init__(content) + self.kwargs['charset']="UTF-8" + diff --git a/students/kbindhu/session08/run_html_render.py b/students/kbindhu/session08/run_html_render.py new file mode 100644 index 0000000..d673449 --- /dev/null +++ b/students/kbindhu/session08/run_html_render.py @@ -0,0 +1,220 @@ +#!/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_kr.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) +# #print(page.content) +# render_page(page, "test_html_output2_krhtml") + +# # 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_kr.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_kr.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_output72.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") diff --git a/students/kbindhu/session08/test_html_output8.html b/students/kbindhu/session08/test_html_output8.html new file mode 100644 index 0000000..23de3f1 --- /dev/null +++ b/students/kbindhu/session08/test_html_output8.html @@ -0,0 +1,31 @@ + + + + + PythonClass = Revision 1087: + + + +

PythonClass - Class 6 example

+ +

+ Here is a paragraph of text -- there could be more of them, but this is enough to show that we can do some text +

+ + + + \ No newline at end of file