HTML/CSS Python - HTML : Isn't this a dictionary ?

  • Thread starter Thread starter mathmari
  • Start date Start date
  • Tags Tags
    Html Python
AI Thread Summary
The discussion revolves around a Python program that retrieves currency exchange data via API calls and aims to display this data using HTML. The endpoint defined returns a rendered HTML template with currency data passed as a list of dictionaries. The main functions involved include retrieving all exchange data from a SQLite database and converting this data into a dictionary format for use in the HTML template.A key point of confusion arises regarding how to correctly access the elements of the exchange data within a loop. The original code mistakenly attempts to access the first element of the `all_exchanges` list instead of the current `exchange` item in the loop. The correct approach is to reference the `exchange` variable directly, allowing for proper extraction of the `id`, `base`, `target`, and `value` fields from each exchange entry. The discussion concludes with a clarification on this coding error, leading to a solution that resolves the issue.
mathmari
Gold Member
MHB
Messages
4,984
Reaction score
7
Hey! 😊

I have written a program in Python where API calls to the server to get currency exchanges. Now I want to add a part for the user, i.e. with html.

The endpoint is :
Code:
@route_rates.route("/all/") 
def get_all(): 
    return render_template("currency.html", my_currency = get_exchange_all_dict())

For this endpoint we need the following functions :
Code:
def get_exchange_all(): 
    con = sqlite3.connect(database_file_name) 
    try : 
        query = f"Select * from Currency" 
        cursor = con.cursor() 
        cursor.execute(query) 
        all_exchanges = cursor.fetchall() 
    except : 
        print("An exception occurred") 
    con.close() 
    return all_exchanges def get_exchange_all_dict(): 
    exchange_list_of_dict = [] 
    all_exchanges = get_exchange_all() 
    for exchange in all_exchanges: 
        exchange_dict = {} 
        exchange_dict["id"] = all_exchanges[0] 
        exchange_dict["base"] = all_exchanges[1] 
        exchange_dict["target"] = all_exchanges[2] 
        exchange_dict["value"] = all_exchanges[3] 
        exchange_list_of_dict.append(exchange_dict) 
    return exchange_list_of_dict
So at 'my_currency' we have a list of dictionaries, right?

At the html part I have written the following so far :
Code:
<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>Currency</title> 
<!-- CSS only -->
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.0-beta1/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-0evHe/X+R7YkIZDRvuzKMRqM+OrBnVFBL6DOitfPri4tjfHxaWutUpFmBp4vmVor" crossorigin="anonymous">    
</head>
<body> 
    <h1><svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-currency-exchange" viewBox="0 0 16 16">
        <path d="M0 5a5.002 5.002 0 0 0 4.027 4.905 6.46 6.46 0 0 1 .544-2.073C3.695 7.536 3.132 6.864 3 5.91h-.5v-.426h.466V5.05c0-.046 0-.093.004-.135H2.5v-.427h.511C3.236 3.24 4.213 2.5 5.681 2.5c.316 0 .59.031.819.085v.733a3.46 3.46 0 0 0-.815-.082c-.919 0-1.538.466-1.734 1.252h1.917v.427h-1.98c-.003.046-.003.097-.003.147v.422h1.983v.427H3.93c.118.602.468 1.03 1.005 1.229a6.5 6.5 0 0 1 4.97-3.113A5.002 5.002 0 0 0 0 5zm16 5.5a5.5 5.5 0 1 1-11 0 5.5 5.5 0 0 1 11 0zm-7.75 1.322c.069.835.746 1.485 1.964 1.562V14h.54v-.62c1.259-.086 1.996-.74 1.996-1.69 0-.865-.563-1.31-1.57-1.54l-.426-.1V8.374c.54.06.884.347.966.745h.948c-.07-.804-.779-1.433-1.914-1.502V7h-.54v.629c-1.076.103-1.808.732-1.808 1.622 0 .787.544 1.288 1.45 1.493l.358.085v1.78c-.554-.08-.92-.376-1.003-.787H8.25zm1.96-1.895c-.532-.12-.82-.364-.82-.732 0-.41.311-.719.824-.809v1.54h-.005zm.622 1.044c.645.145.943.38.943.796 0 .474-.37.8-1.02.86v-1.674l.077.018z"/>
      </svg></h1> 
    {% for exchange in my_currency %} 
        <div> 
            <label>id : {{exchange["id"]}}</label> 
            <label>base : {{exchange["base"]}}</label> 
            <label>target : {{exchange["target"]}}</label> 
            <label>value : {{exchange["value"]}}</label>
        </div> 
    {% endfor %}
<!-- JavaScript Bundle with Popper -->
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.2.0-beta1/dist/js/bootstrap.bundle.min.js" integrity="sha384-pprn3073KE6tl6bjs2QrFaJGz5/SUsLqktiwsUTF55Jfv3qYSDhgCecCxMW52nD2" crossorigin="anonymous"></script>    
</body>
</html>

I get :
1655932703662.png
Isn't each "exchange" a dictionary? How can I get for the first one for example :
id : 0 , base : 'CHF' , target : 'CHF' , value : 1.0
etc

What is wrong at my code ? :unsure:
 
Technology news on Phys.org
Hey mathmari!

You have:
Code:
   for exchange in all_exchanges: 
        exchange_dict = {} 
        exchange_dict["id"] = all_exchanges[0]
Perhaps it should be something like:
Code:
   for exchange in all_exchanges: 
        exchange_dict = {} 
        exchange_dict["id"] = exchange[0]
(Wondering)
 
Klaas van Aarsen said:
You have:
Code:
   for exchange in all_exchanges:
        exchange_dict = {}
        exchange_dict["id"] = all_exchanges[0]
Perhaps it should be something like:
Code:
   for exchange in all_exchanges:
        exchange_dict = {}
        exchange_dict["id"] = exchange[0]
(Wondering)
Ah yes! (Tmi)

Thank you! 😊
 
Thread 'Star maps using Blender'
Blender just recently dropped a new version, 4.5(with 5.0 on the horizon), and within it was a new feature for which I immediately thought of a use for. The new feature was a .csv importer for Geometry nodes. Geometry nodes are a method of modelling that uses a node tree to create 3D models which offers more flexibility than straight modeling does. The .csv importer node allows you to bring in a .csv file and use the data in it to control aspects of your model. So for example, if you...
I tried a web search "the loss of programming ", and found an article saying that all aspects of writing, developing, and testing software programs will one day all be handled through artificial intelligence. One must wonder then, who is responsible. WHO is responsible for any problems, bugs, deficiencies, or whatever malfunctions which the programs make their users endure? Things may work wrong however the "wrong" happens. AI needs to fix the problems for the users. Any way to...

Similar threads

Replies
5
Views
2K
Back
Top