简体   繁体   中英

print hierarchy python dictionary in flask jinja

I have nested dictionary in python:

result = {'Physics': {'Mechanics': {'Kinematics ': {}, 'Dynamics': {}}}, 'Math': {'Algebra': {'Polynomials': {'sum of polynomials': {}}}}}

I need to print in flask/jinja this data. I want to get a tree or a nested list. The result should be:

<ul class="list">
   <li class="title">
      <a href="/">"Physics"</a>
   </li>
   <ul class="list2">
      <li class="title2">
         <a href="/">"Mechanics"</a>
      </li>
      <ul class="list3">
         <li class="title3">
            <a href="/">"Kinematics "</a>
         </li>
         <li class="title3">
            <a href="/">'Dynamics'</a>
         </li>
      </ul>
    </ul>
 </ul>

<ul class="list">
   <li class="title">
      <a href="/">'Math'</a>
   </li>
   <ul class="list2">
      <li class="title2">
         <a href="/">'Algebra'</a>
      </li>
      <ul class="list3">
         <li class="title3">
            <a href="/">'Polynomials'</a>
         </li>
         <ul class="list4">
            <li class="title4">
               <a href="/">'sum of polynomials'</a>
            </li>
         </ul>
      </ul>
    </ul>
 </ul>

How should I solve my problem?

You can use recursion to create the HTML markup of the dictionary, and then pass the resulting string to your template:

def to_html(d, c = 1):
   if not all(d.values()):
      return f'<ul class="list{"" if c == 1 else c}">'+'\n'.join(f'<li class="title{"" if c == 1 else c}"><a href="/">{a}</a></li>{"" if not b else to_html(b, c+1)}' for a, b in d.items())+'</ul>'
   return '\n'.join(f'<li class="title{"" if c == 1 else c}"><a href="/">{a}</a></li>' if not b else \
         f"""
         <ul class="list{"" if c == 1 else c}">
            <li class="title{"" if c == 1 else c}"><a href="/">{a}</a></li>
            {to_html(b, c+1)}
         </ul>
         """
         for a, b in d.items())

d = {'Physics': {'Mechanics': {'Kinematics ': {}, 'Dynamics': {}}}, 'Math': {'Algebra': {'Polynomials': {'sum of polynomials': {}}}}}
print(to_html(d))

Output:

<ul class="list">
 <li class="title">
  <a href="/">
   Physics
  </a>
 </li>
 <ul class="list2">
  <li class="title2">
   <a href="/">
    Mechanics
   </a>
  </li>
  <ul class="list3">
   <li class="title3">
    <a href="/">
     Kinematics
    </a>
   </li>
   <li class="title3">
    <a href="/">
     Dynamics
    </a>
   </li>
  </ul>
 </ul>
</ul>
<ul class="list">
 <li class="title">
  <a href="/">
   Math
  </a>
 </li>
 <ul class="list2">
  <li class="title2">
   <a href="/">
    Algebra
   </a>
  </li>
  <ul class="list3">
   <li class="title3">
    <a href="/">
     Polynomials
    </a>
   </li>
   <ul class="list4">
    <li class="title4">
     <a href="/">
      sum of polynomials
     </a>
    </li>
   </ul>
  </ul>
 </ul>
</ul>

Then, in flask :

@app.route('/hierarchy', methods=['GET'])
def hierarchy():
    d = {'Physics': {'Mechanics': {'Kinematics ': {}, 'Dynamics': {}}}, 'Math': {'Algebra': {'Polynomials': {'sum of polynomials': {}}}}}
    return flask.render_template('show_hierarchy.html', hierarchy=to_html(d))

show_hierarchy.html :

<h1>Hierarchy</h1>
{{hierarchy}}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM