Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
convert a list to links
#1
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
Reply
#2
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/>
Pir8Radio likes this post
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#3
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.
Pir8Radio likes this post
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#4
(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!!!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  convert string to float in list jacklee26 6 1,936 Feb-13-2023, 01:14 AM
Last Post: jacklee26
  convert this List Comprehensions to loop jacklee26 8 1,534 Oct-21-2022, 04:25 PM
Last Post: deanhystad
  Convert list to interger Clives 5 1,640 May-09-2022, 12:53 PM
Last Post: deanhystad
  Convert each element of a list to a string for processing tester_V 6 5,362 Jun-16-2021, 02:11 AM
Last Post: tester_V
  convert numbers into list lokesh 1 2,390 Jun-03-2021, 06:37 AM
Last Post: menator01
Question convert unlabeled list of tuples to json (string) masterAndreas 4 7,483 Apr-27-2021, 10:35 AM
Last Post: masterAndreas
Star Convert Bytearray into List using list() Shlok 2 4,176 Feb-18-2021, 10:44 AM
Last Post: deanhystad
  convert List with dictionaries to a single dictionary iamaghost 3 2,880 Jan-22-2021, 03:56 PM
Last Post: iamaghost
  help with url links- href links don't work properly DeBug_0neZer0 1 1,987 Jan-06-2021, 11:01 PM
Last Post: DeBug_0neZer0
Sad Convert python list to dictionary akanowhere 6 3,471 Dec-27-2020, 09:26 PM
Last Post: Pedroski55

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020