Python Forum

Full Version: convert a list to links
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
hi I am currently using this javascript and would love to do it natively in python... can someone lend me a hand getting the same result as I do with the JS but server side? instead of outputting the string list I want to output the html and string numbers.

<script type="text/javascript"> const out = {{ blk.ats|blkatid|safe }}.reduce((prev, item) => prev += <a href=/at/${item}>${item}</a><br/>, ''); document.write(out);</script>

This takes a python string like this: ['12227509686562344891', '14231167159403602480'] and gives me an html output in django like this:
<a href="/at/12227509686562344891">12227509686562344891</a><br/><a href="/at/14231167159403602480">14231167159403602480</a><br/>
So the above string is what i need the python script to output.. the original string list can be one or many.

for an html output similar to this:
12227509686562344891
14231167159403602480
I have little idea what other parts of this code do but constructing links part is relatively easy: create links generator and consume it by joining items to string:

nums = ['12227509686562344891', '14231167159403602480']
links = (f'<a href="/at/{num}">{num}</a><br/>' for num in nums)
astext = ''.join(links)

print(astext)


# output
<a href="/at/12227509686562344891">12227509686562344891</a><br/><a href="/at/14231167159403602480">14231167159403602480</a><br/>
You can use a Templating language like jinja2.

import subprocess
from jinja2 import Environment, DictLoader

html_index = """<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title>{{ title }}</title>
  </head>

  <body>
  <h1>{{ topic }}</h1>
  {% include 'body.html' %}
  </body>
</html>"""

html_body = """  <ul>
    {%- for num in nums %}
      <li><a href="/at/{{ num }}">{{ num }}</a></li>
    {%- endfor %}
    </ul>"""

nums = ["12227509686562344891", "14231167159403602480"]
env = Environment(loader=DictLoader({"index.html": html_index, "body.html": html_body}))
nums = ["12227509686562344891", "14231167159403602480"]

with open("index.html", "w") as fd:
    fd.write(
        env.get_template("index.html").render(
            nums=nums, title="Index", topic="unordered list"
        )
    )
    subprocess.check_call(["xdg-open", "index.html"])
Usually jinja2 is used together with Flask and does not require the dictloader.
Instead, it loads local files from templates directory.
(Nov-28-2022, 07:28 AM)perfringo Wrote: [ -> ]I have little idea what other parts of this code do but constructing links part is relatively easy: create links generator and consume it by joining items to string:

nums = ['12227509686562344891', '14231167159403602480']
links = (f'<a href="/at/{num}">{num}</a><br/>' for num in nums)
astext = ''.join(links)

print(astext)

# output
<a href="/at/12227509686562344891">12227509686562344891</a><br/><a href="/at/14231167159403602480">14231167159403602480</a><br/>

Thank you, this is what i was looking for... appreciate the responses guys!!!