Build unordered list in HTML from a python list
Dear list members I have this python list that represets a sitemap: tree = [{'indent': 1, 'title':'Item 1', 'hassubfolder':False}, {'indent': 1, 'title':'Item 2', 'hassubfolder':False}, {'indent': 1, 'title':'Folder 1', 'hassubfolder':True}, {'indent': 2, 'title':'Sub Item 1.1', 'hassubfolder':False}, {'indent': 2, 'title':'Sub Item 1.2', 'hassubfolder':False}, {'indent': 1, 'title':'Item 3', 'hassubfolder':False}, {'indent': 1, 'title':'Folder 2', 'hassubfolder':True}, {'indent': 2, 'title':'Sub Item 2.1', 'hassubfolder':False}, {'indent': 2, 'title':'Folder 2.1', 'hassubfolder':True}, {'indent': 3, 'title':'Sub Item 2.1.1', 'hassubfolder':False}, {'indent': 3, 'title':'Sub Item 2.1.2', 'hassubfolder':False}, ] From that list I want to create the following HTML code: ul id=tree liItem 1/li liItem 2/li liFolder 1 ul liSub Item 1.1/li liSub Item 1.2/li /ul /li liItem 3/li liFolder 2 ul liSub Item 2.1/li liFolder 2.1 ul liSub Item 2.1.1/li liSub Item 2.1.2/li /ul /li /ul /li /ul If an item of the list has 'True' for the 'hassubfolder' key than a new ulli must be created instead of /li after its title. (See Folder 2 node in the HTML code above. My problem is: How do I keep track of the closing tags while iterating over the python list? Any help is much appreciated. Regards Nico -- http://mail.python.org/mailman/listinfo/python-list
Re: Build unordered list in HTML from a python list
On Wed, Jun 30, 2010 at 2:04 PM, Nico Grubert nicogrub...@yahoo.de wrote: Dear list members I have this python list that represets a sitemap: tree = [{'indent': 1, 'title':'Item 1', 'hassubfolder':False}, {'indent': 1, 'title':'Item 2', 'hassubfolder':False}, {'indent': 1, 'title':'Folder 1', 'hassubfolder':True}, {'indent': 2, 'title':'Sub Item 1.1', 'hassubfolder':False}, {'indent': 2, 'title':'Sub Item 1.2', 'hassubfolder':False}, {'indent': 1, 'title':'Item 3', 'hassubfolder':False}, {'indent': 1, 'title':'Folder 2', 'hassubfolder':True}, {'indent': 2, 'title':'Sub Item 2.1', 'hassubfolder':False}, {'indent': 2, 'title':'Folder 2.1', 'hassubfolder':True}, {'indent': 3, 'title':'Sub Item 2.1.1', 'hassubfolder':False}, {'indent': 3, 'title':'Sub Item 2.1.2', 'hassubfolder':False}, ] From that list I want to create the following HTML code: ul id=tree liItem 1/li liItem 2/li liFolder 1 ul liSub Item 1.1/li liSub Item 1.2/li /ul /li liItem 3/li liFolder 2 ul liSub Item 2.1/li liFolder 2.1 ul liSub Item 2.1.1/li liSub Item 2.1.2/li /ul /li /ul /li /ul If an item of the list has 'True' for the 'hassubfolder' key than a new ulli must be created instead of /li after its title. (See Folder 2 node in the HTML code above. My problem is: How do I keep track of the closing tags while iterating over the python list? Use a stack? Whenever you start a new list, push the corresponding closing tag onto a stack. Whenever your indent level decreases, pop the stack and write out the closing tag you get. It's straightforward to use a python list as a stack. -- regards, kushal -- http://mail.python.org/mailman/listinfo/python-list
Re: Build unordered list in HTML from a python list
Nico Grubert, 30.06.2010 10:34: I have this python list that represets a sitemap: tree = [{'indent': 1, 'title':'Item 1', 'hassubfolder':False}, {'indent': 1, 'title':'Item 2', 'hassubfolder':False}, {'indent': 1, 'title':'Folder 1', 'hassubfolder':True}, {'indent': 2, 'title':'Sub Item 1.1', 'hassubfolder':False}, {'indent': 2, 'title':'Sub Item 1.2', 'hassubfolder':False}, {'indent': 1, 'title':'Item 3', 'hassubfolder':False}, {'indent': 1, 'title':'Folder 2', 'hassubfolder':True}, {'indent': 2, 'title':'Sub Item 2.1', 'hassubfolder':False}, {'indent': 2, 'title':'Folder 2.1', 'hassubfolder':True}, {'indent': 3, 'title':'Sub Item 2.1.1', 'hassubfolder':False}, {'indent': 3, 'title':'Sub Item 2.1.2', 'hassubfolder':False}, ] From that list I want to create the following HTML code: ul id=tree liItem 1/li liItem 2/li liFolder 1 ul liSub Item 1.1/li liSub Item 1.2/li /ul /li liItem 3/li liFolder 2 ul liSub Item 2.1/li liFolder 2.1 ul liSub Item 2.1.1/li liSub Item 2.1.2/li /ul /li /ul /li /ul If an item of the list has 'True' for the 'hassubfolder' key than a new ulli must be created instead of /li after its title. (See Folder 2 node in the HTML code above. My problem is: How do I keep track of the closing tags while iterating over the python list? Don't. Just use a tool for generating the XML, such as ElementTree's builder. http://effbot.org/zone/element-builder.htm http://svn.effbot.org/public/stuff/sandbox/elementlib/builder.py Stefan -- http://mail.python.org/mailman/listinfo/python-list
Re: Build unordered list in HTML from a python list
Use a stack? Whenever you start a new list, push the corresponding closing tag onto a stack. Whenever your indent level decreases, pop the stack and write out the closing tag you get. It's straightforward to use a python list as a stack. Thanks for the tip, Kushal. Do you have a short code example for me? Regards Nico -- http://mail.python.org/mailman/listinfo/python-list
Re: Build unordered list in HTML from a python list
Dear list members I have this python list that represets a sitemap: tree = [{'indent': 1, 'title':'Item 1', 'hassubfolder':False}, {'indent': 1, 'title':'Item 2', 'hassubfolder':False}, {'indent': 1, 'title':'Folder 1', 'hassubfolder':True}, {'indent': 2, 'title':'Sub Item 1.1', 'hassubfolder':False}, {'indent': 2, 'title':'Sub Item 1.2', 'hassubfolder':False}, {'indent': 1, 'title':'Item 3', 'hassubfolder':False}, {'indent': 1, 'title':'Folder 2', 'hassubfolder':True}, {'indent': 2, 'title':'Sub Item 2.1', 'hassubfolder':False}, {'indent': 2, 'title':'Folder 2.1', 'hassubfolder':True}, {'indent': 3, 'title':'Sub Item 2.1.1', 'hassubfolder':False}, {'indent': 3, 'title':'Sub Item 2.1.2', 'hassubfolder':False}, If an item of the list has 'True' for the 'hassubfolder' key than a new ulli must be created instead of /li after its title. (See Folder 2 node in the HTML code above. Or you can change your list to a dict with a tree structure and use recursion instead of a stack: tree = [ { 'title': 'Item 1', 'children': [] }, { 'title': 'Item 2', 'children': [] }, { 'title': 'Folder 1', 'children': [ { 'title': 'sub item 1.1', 'children' : [], }, { 'title': 'Folder 1.1', 'children' : [ .. ], }, ] }, ] and something like: def displayListItem(i): print 'li%s' % i['title'] if i['children']: print 'ul' for c in i['children']: displayListItem(c) print '/ul' print /li you might also use recursion with your existing list, by 'pop'ing the first item and passing the rest of the list. Remi -- http://mail.python.org/mailman/listinfo/python-list
Re: Build unordered list in HTML from a python list
Nico Grubert wrote: Use a stack? Whenever you start a new list, push the corresponding closing tag onto a stack. Whenever your indent level decreases, pop the stack and write out the closing tag you get. It's straightforward to use a python list as a stack. Thanks for the tip, Kushal. Do you have a short code example for me? Regards Nico mylist = [3, 4, 5] mylist.append[42] print mylist3,4,5,42 item = mylist.pop() returns the 42, removing it from the list So, use append as a push, and pop without arguments as a pop. And use len() to decide how big the list currently is. DaveA -- http://mail.python.org/mailman/listinfo/python-list
Re: Build unordered list in HTML from a python list
On Wed, 2010-06-30, Kushal Kumaran wrote: On Wed, Jun 30, 2010 at 2:04 PM, Nico Grubert nicogrub...@yahoo.de wrote: Dear list members I have this python list that represets a sitemap: tree = [{'indent': 1, 'title':'Item 1', 'hassubfolder':False}, {'indent': 1, 'title':'Item 2', 'hassubfolder':False}, {'indent': 1, 'title':'Folder 1', 'hassubfolder':True}, {'indent': 2, 'title':'Sub Item 1.1', 'hassubfolder':False}, {'indent': 2, 'title':'Sub Item 1.2', 'hassubfolder':False}, {'indent': 1, 'title':'Item 3', 'hassubfolder':False}, {'indent': 1, 'title':'Folder 2', 'hassubfolder':True}, {'indent': 2, 'title':'Sub Item 2.1', 'hassubfolder':False}, {'indent': 2, 'title':'Folder 2.1', 'hassubfolder':True}, {'indent': 3, 'title':'Sub Item 2.1.1', 'hassubfolder':False}, {'indent': 3, 'title':'Sub Item 2.1.2', 'hassubfolder':False}, ] From that list I want to create the following HTML code: ul id=tree liItem 1/li liItem 2/li liFolder 1 ul liSub Item 1.1/li liSub Item 1.2/li /ul /li liItem 3/li liFolder 2 ul liSub Item 2.1/li liFolder 2.1 ul liSub Item 2.1.1/li liSub Item 2.1.2/li /ul /li /ul /li /ul If an item of the list has 'True' for the 'hassubfolder' key than a new ulli must be created instead of /li after its title. (See Folder 2 node in the HTML code above. My problem is: How do I keep track of the closing tags while iterating over the python list? Use a stack? Whenever you start a new list, push the corresponding closing tag onto a stack. Whenever your indent level decreases, pop the stack and write out the closing tag you get. It's straightforward to use a python list as a stack. Or even simpler. You keep track of your current indentation level (0, 1, ...). If level==1 and you come to an indent: 2, you generate an ul and increase level to 2. Similarly for going left. When you reach the end you add /uls to go back up to level 1 (or maybe you want to call it level 0 instead). That probably assumes you use HTML (like you say) rather than XHTML (which your example hints at). In HTML you don't need to supply the /lis. I did all this in Perl earlier today, but in my case it was unsuitable because I skipped levels (had a list of HTML headings, wanted a table-of-contents, but sometimes a h1 was followed by h3 with no h2 inbetween. I'd get invalid stuff like ulul. /Jorgen -- // Jorgen Grahn grahn@ Oo o. . . \X/ snipabacken.se O o . -- http://mail.python.org/mailman/listinfo/python-list
Re: Build unordered list in HTML from a python list
I have this python list that represets a sitemap: tree = [{'indent': 1, 'title':'Item 1', 'hassubfolder':False}, {'indent': 1, 'title':'Item 2', 'hassubfolder':False}, {'indent': 1, 'title':'Folder 1', 'hassubfolder':True}, {'indent': 2, 'title':'Sub Item 1.1', 'hassubfolder':False}, {'indent': 2, 'title':'Sub Item 1.2', 'hassubfolder':False}, {'indent': 1, 'title':'Item 3', 'hassubfolder':False}, {'indent': 1, 'title':'Folder 2', 'hassubfolder':True}, {'indent': 2, 'title':'Sub Item 2.1', 'hassubfolder':False}, {'indent': 2, 'title':'Folder 2.1', 'hassubfolder':True}, {'indent': 3, 'title':'Sub Item 2.1.1', 'hassubfolder':False}, {'indent': 3, 'title':'Sub Item 2.1.2', 'hassubfolder':False}, ] From that list I want to create the following HTML code: ul id=tree liItem 1/li liItem 2/li liFolder 1 ul liSub Item 1.1/li liSub Item 1.2/li /ul /li liItem 3/li liFolder 2 ul liSub Item 2.1/li liFolder 2.1 ul liSub Item 2.1.1/li liSub Item 2.1.2/li /ul /li /ul /li /ul If an item of the list has 'True' for the 'hassubfolder' key than a new ulli must be created instead of /li after its title. (See Folder 2 node in the HTML code above. My problem is: How do I keep track of the closing tags while iterating over the python list? Elementtree has already been mentioned as a solution, but if you need something more lightweight, give markup.py a try: http://markup.sourceforge.net/ it's a single module and very easy to use for the sort of things you are after. Cheers, Daniel -- Psss, psss, put it down! - http://www.cafepress.com/putitdown -- http://mail.python.org/mailman/listinfo/python-list