How to store a portion of a result in python?

Question:

I have this code to get download links via libgen api.

@app.route("/api/default")
def titleSearch():
    title = request.args.get('query')

    
    s = LibgenSearch()
    results = s.search_title(title)
    item_to_download = results[0]
    download_links = s.resolve_download_links(item_to_download)
    return render_template(
        "results.html", results=results, download_links=download_links, title=title,
    )

results.html

<!DOCTYPE html>
<html lang="en">

<head>


<meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>SUSBOOKS</title>
     
    <link rel="stylesheet" href="{{ url_for('static', filename='bootstrap.min.css') }}" />
    <link rel="stylesheet" href="{{ url_for('static', filename='mdb.dark.min.css') }}" />
    <link rel="stylesheet" href="{{ url_for('static', filename='all.min.css') }}" />
    
    <style>
      body {
  background-color:#000000 ;
      }

</style>
  
</head>
<body>
    <p>{{download_links}}</p>

</body>

download_links comes out as

{'GET': 'http://62.182.86.140/main/265000/ef7a3a2ecf51fa41f621f4e8b17df828/%28Saddleback%27s%20Focus%20on%20Reading%20Study%20Guides%29%20Lisa%20Mccarty%20-%20To%20Kill%20a%20Mockingbird-Saddleback%20Educational%20Publishing%2C%20Inc.%20%282006%29.pdf', 'Cloudflare': 'https://cloudflare-ipfs.com/ipfs/bafykbzacebtnktd2zpvrcygswfcsr6wfft6jjyes6qi7f4mmottq3lebrgpsc?filename=%28Saddleback%27s%20Focus%20on%20Reading%20Study%20Guides%29%20Lisa%20Mccarty%20-%20To%20Kill%20a%20Mockingbird-Saddleback%20Educational%20Publishing%2C%20Inc.%20%282006%29.pdf', 'IPFS.io': 'https://ipfs.io/ipfs/bafykbzacebtnktd2zpvrcygswfcsr6wfft6jjyes6qi7f4mmottq3lebrgpsc?filename=%28Saddleback%27s%20Focus%20on%20Reading%20Study%20Guides%29%20Lisa%20Mccarty%20-%20To%20Kill%20a%20Mockingbird-Saddleback%20Educational%20Publishing%2C%20Inc.%20%282006%29.pdf'}{'GET': 'http://62.182.86.140/main/265000/ef7a3a2ecf51fa41f621f4e8b17df828/%28Saddleback%27s%20Focus%20on%20Reading%20Study%20Guides%29%20Lisa%20Mccarty%20-%20To%20Kill%20a%20Mockingbird-Saddleback%20Educational%20Publishing%2C%20Inc.%20%282006%29.pdf', 'Cloudflare': 'https://cloudflare-ipfs.com/ipfs/bafykbzacebtnktd2zpvrcygswfcsr6wfft6jjyes6qi7f4mmottq3lebrgpsc?filename=%28Saddleback%27s%20Focus%20on%20Reading%20Study%20Guides%29%20Lisa%20Mccarty%20-%20To%20Kill%20a%20Mockingbird-Saddleback%20Educational%20Publishing%2C%20Inc.%20%282006%29.pdf', 'IPFS.io': 'https://ipfs.io/ipfs/bafykbzacebtnktd2zpvrcygswfcsr6wfft6jjyes6qi7f4mmottq3lebrgpsc?filename=%28Saddleback%27s%20Focus%20on%20Reading%20Study%20Guides%29%20Lisa%20Mccarty%20-%20To%20Kill%20a%20Mockingbird-Saddleback%20Educational%20Publishing%2C%20Inc.%20%282006%29.pdf'}

How would I go about making it output just the links hyperlinked so users can just click to download them?

Asked By: Samurai6465

||

Answers:

In response, you have a dictionary which is key-value pairs.
reading key value of a dictionary:

my_dict = {
    "link_1": "example_1.com",
    "link_2": "example_2.com" 
    }
for key, value in my_dict.items():
    print(key, value)

output

link_1, example_1.com
link_2, example_2.com

now you flask template you can do

{% for key, value in my_dict.items() %}
    <p>{{ key }}, {{ value }}</p>
{% endfor %}

instead of
<p>{{ my_dict }}</p>

Answered By: umair mehmood
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.