简体   繁体   中英

How do i display values from the same index position from multiple python lists, on 1 line, in HTML using Jinja 2?

So for an assignment, ive been tasked with building a program that displays live train information based on the data inputted by the user (Station they want to go from, station they want to go to, date and time). When this information is gathered, and parsed into the api, i grab data such as the platform number, departure time, train company etc, and put each value into its own list, in its own index position (for example, each platform number will go into a list called platformnumber, in its own position ['2', '12', '4']).

Once i have all this data in the lists, because of the way i have programmed it, all the data at index[0] in all the lists, matches up together, same with index[1] and so on. My issue is, i need this data to be displayed on my HTML page, with each train showing the platform number, operator name and the departure time on its OWN line. Below is the code i have so far, and a screenshot of how far i have got so far.

I managed to get so far, by looping through the platform list, and making a new paragraph for every platform (there will always be the same amount of platforms as trains), which is good because i have the right amount of lines i need. However because the rest of my data is also in lists, i cant just keep adding loops inside loops to get the data out. Thats my issue.

Below is the code i use to store the values from the api to the lists. This stores each trains data, in its own index position in each list (say the first train's data will all be in index 0 of each list). (some of the variable in here you can ignore as they are not relevant for this issue)

        date = parsed["date"]
        stationname = parsed["station_name"]
        time = parsed["time_of_day"]
        departures = parsed["departures"]
        platformnumber = []
        destinationstation = parsed["departures"]["all"][0]["destination_name"]
        operatorname = []
        departuretime = []
        trainuid = []

        for i in departures["all"]:
            for k, v in i.items():
                if k == "platform":
                    platformnumber.append(v)
                elif k == "aimed_departure_time":
                    departuretime.append(v)
                elif k == "operator_name":
                    operatorname.append(v)
                elif k == "train_uid":
                    trainuid.append(v)

return render_template('timetable.html', date=date, station=stationname, time=time, destination=destinationstation, operator=operatorname,departuretime=departuretime, platform=platformnumber, liveoperatorname=liveoperatorname, livedeparturetime=livedeparturetime, liveplatformnumber=liveplatformnumber, livedestination=livedestination, callingat=destination)

Here is my Jinja 2/HTML code that i have got so far. Hopefully you can see what im trying to do, and why i am getting stuck.

{% for a in platform %}
    <li>Platform {{a}} For The {{departuretime}} {{operator}} Service To {{destination}}</li>
    {% endfor %}

What i want, is to have a line for each train, looking something like:

Platform 12 For The 14:25 South Western Railway Service to London Waterloo

But instead it displays like this, as i dont know how to get the data from the other lists (The line below is printed for the amount of platform there are in the list, which is the same as the amount of trains as every train has a platform).

Platform 2 For The ['12:22', '12:59', '13:05', '13:22', '13:59', '14:05'] ['South Western Railway', 'South Western Railway', 'South Western Railway', 'South Western Railway', 'South Western Railway', 'South Western Railway'] Service To London Waterloo.

The way you did, you can use loop.index to get the index of the current loop. Be careful, because it starts at 1, not 0 . Then you have to do my_list[loop.index-1]

Your Jinga2 template should look like :

{% for a in list_a %}
    <li>Platform {{a}} For The {{list_b[loop.index-1]}} and {{list_c[loop.index-1]}}</li>
{% endfor %}

But a better solution would be to use a different design. You don't need to create 2, 3 or 4 lists. You can just create a table (list of list). That looks like :

information = [
    ["plaform A", "departure A", ...],
    ["plaform B", "departure B", ...],
]

Then in your Jinja2 template you can just access your table like that :

{% for platform, departure, ... in information %}
    <li>Platform {{platform}} For The {{departure}}</li>
{% endfor %}

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