简体   繁体   中英

How to associate dictionnary keys with description using Flask

I am using Flask to build my web application. I have a dictionnary with some key in it, unfortunatly these keys are not easily readable for a user. I'd like to display nicer name and a descriptions sentence for each keys.

The display of my keys is in jinja2 loop, iterating over the dictionnary.

My actual page:

key_1 : value_1
key_2 : value_2

The page I want to have:

Nice name for key1 (Description of the key1) : value_1
Nice name for key2 (Description of the key2) : value_2

I already look in flask-babel extension, but it does not seems to fit since the keys in jinja2 template will be displayed at runtime. Even if I am manually creating a .po file with all possible keys, I doubt it will correctly work.

I was thinking to use a database to make the correspondance, but I think it might not be the nicest way to do that

key_1 = { name : "Nice name for key1"
          description : "Nice description for key1"
         }

What do you think would be the best way of doing such correspondance ?

EDIT: My keys are coming from a dictionnary passed to the template from flask. I dont think from where the dictionnary is coming is important.

So It looks like return render_template(mypage.html, dictionnary=dictionnary)

I am not really trying to translate my nice name and description in a first time, just make a link between the keys of the dictionnary and names and descriptions to display which are understandable to users. I though using flask-babel cause it first looks like a way of doing such links. Unfortunatly I think because my keys are generating at runtime with jinja2 it will not work.

{% for key,value dictionnary.iteritems()%}
<li> {{ key }}, {{value}} </li>
{% endfor %}

Use babel/flask-babel if you want to translate your content in different languages.

It isn't entirely clear for me what you're trying to achieve here.

  1. Where do you get the keys from?
  2. Do you want to translate your nice name and descriptions?

Although, if you are looking to convert some string like 'weight_of_the_sun' to 'Weight of the sun' , you could consider using inflection

Use inflection.humanize

import inflection

print(inflection.humanize('weight_of_the_sun'))
# Prints: Weight of the sun

Depending on what you want to do, some other function might work also.

EDIT: Don't bother looking into babel if you aren't translating anything.

I would recommend some structure like:

your_dict = {
    'key_1': {
        'name': "Whatever name",
        'value': Whatever value,
        'description': "Whatever description"
    }, ...
}

Use this like,

{% for key, struct in your_dict.iteritems() %}
  <li>{{ key }} {{ struct['name'] }} {{ struct['description'] }} {{ struct['value'] }}</li>
{% endfor %}

OR,

your_list = [
    {
        'key': 'key_1',
        'name': "Whatever name",
        'value': Whatever value,
        'description': "Whatever description"
    }, ...
]

NOTE: I like this better because you can order a list .

Use this like,

{% for struct in your_list %}
  <li>{{ struct['key'] }} {{ struct['name'] }} {{ struct['description'] }} {{ struct['value'] }}</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