Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get value of the nested dictionary using ImmutableMultiDict on Flask?

address_dict = {'address': {'US': 'San Francisco', 'US': 'New York', 'UK': 'London'}}

When above parameters was sent via requests, how can I get values in address key using request.form on Flask?

import requests
url =  'http://example.com'
params = {"address": {"US": "San Francisco", "UK": "London", "CH": "Shanghai"}}
requests.post(url, data=params) 

Then I got this in context of flask.request.

ImmutableMultiDict([('address', u'US'), ('address', 'US'), ('address', 'UK')])

How can I get the value in each key of addresses?

Thanks.

like image 598
kinakomochi Avatar asked May 21 '12 08:05

kinakomochi


People also ask

How do you get a value in a nested dictionary Python?

Access Values using get() Another way to access value(s) in a nested dictionary ( employees ) is to use the dict. get() method. This method returns the value for a specified key. If the specified key does not exist, the get() method returns None (preventing a KeyError ).

How do you assign a value to a nested dictionary?

Adding elements to a Nested Dictionary One way to add a dictionary in the Nested dictionary is to add values one be one, Nested_dict[dict][key] = 'value'. Another way is to add the whole dictionary in one go, Nested_dict[dict] = { 'key': 'value'}.

What is ImmutableMultiDict?

In this article, we will see how to get data from ImmutableMultiDict in the flask. It is a type of Dictionary in which a single key can have different values. It is used because some elements have multiple values for the same key and it saves the multiple values of a key in form of a list.

How do I create a nested dictionary?

You can create a nested dictionary in Python by placing comma-separated dictionaries within curly braces {}. A Python nested dictionary allows you to store and access data using the key-value mapping structure within an existing dictionary.


2 Answers

for example like this:

from werkzeug.datastructures import ImmutableMultiDict
imd = ImmutableMultiDict([('address', u'US'), ('address', 'US'), ('address', 'UK')])
print imd.getlist('address')

prints:

[u'US', 'US', 'UK']

edit:

Your POST-request is sent application/x-www-form-urlencoded, which means as combination as key/value pairs. It doesn't support a nested dict structure directly. When i try your curl-request I get this:

ImmutableMultiDict([('address[US]', u'San Francisco'), ('address[US]', u'New York'), ('address[UK]', u'London')])

so the keys are interpreted literally here.

and using urllib2 i get this result:

>>> print urllib2.urlopen("http://localhost:5000/post", data=urllib.urlencode(address_dict)).read()
ImmutableMultiDict([('address', u"{'UK': 'London', 'US': 'New York'}")])

here urlencode simply sends a string representation of the inner dict.

and finally using requests:

>>> print requests.post("http://localhost:5000/post", data=address_dict).content
ImmutableMultiDict([('address', u'UK'), ('address', u'US')])

here the array is flattened and recurring keys eliminated.

There is simply no defined way how to send a nested dict like yours in an urlencoded way, so you'll have to find another solution.

like image 147
mata Avatar answered Sep 18 '22 05:09

mata


You gave this example:

curl http://example.com -d "address[US]=San Francisco" -d "address[US]=New York" -d address[UK]=London"

With such a request, request.form would look like this:

>>> request.form
ImmutableMultiDict([('address[US]', u'San Francisco'), ('address[US]', u'New York'),
                    ('address[UK]', u'London')])
>>> request.form['address[US]']
u'San Francisco'
>>> request.form.getlist('address[US]')
[u'San Francisco', u'New York']

The [ and ] characters are not special, they are just part of the keys. As documented, a MultiDict will map each key not just to one value but to a list of values. Normal dictionnary access with [] will only give you the first key, but the getlist() method gives the whole list for one key. Other methods are available, see the docs.

If you really want a nested structure, try JSON instead of flat form data.

like image 32
Simon Sapin Avatar answered Sep 21 '22 05:09

Simon Sapin