Hi all,
The following http request using anvil.http.request returns dictionary out of order:
resp_hist = anvil.http.request(‘https://corona.lmao.ninja/v2/historical/south%20africa’,json=True)
Dates in dict is not ordered as per JSON data.
When I do the same with requests library I get the data in dict format in correct order.
import requests
req = requests.get('https://corona.lmao.ninja/v2/historical/south%20africa')
data = req.json()
Any simple way to get the dict ordered in anvil.http.request?
how about something like this:
from collections import OrderedDict
from datetime import datetime
data = anvil.http.request('https://corona.lmao.ninja/v2/historical/south%20africa', json=True)
cases = data['timeline']['cases']
cases = OrderedDict(sorted(cases.items(), key=lambda t: datetime.strptime(t[0], '%m/%d/%y')))
I think they would possibly be ordered in the full python environment… but not likely to be ordered in basic python environment… since builtin dictionaries only became officially ordered in python 3.7 (I believe)
Thank you,
Something interesting though. I run this on ServerModule in def and return cases:
@anvil.server.callable
def get_history():
resp_hist = anvil.http.request('https://corona.lmao.ninja/v2/historical/south%20africa',json=True)
cases = resp_hist['timeline']['cases']
cases = OrderedDict(sorted(cases.items(), key=lambda t: datetime.strptime(t[0], '%m/%d/%y')))
return cases
On the client side I call the function:
history = anvil.server.call(‘get_history’)
I get the unordered dict again. When I try to create ordered dict client side code I get following:

Suppose I can create two lists from the ordered dict server side and pass the lists back?
wow you’ve really dug into some underlying architecture with your first post!
skulpt is the client side python interpreter and the datetime module is a little basic so… yeah
here’s a solution with OrderedDict
Server Side
import anvil.http
from collections import OrderedDict
from datetime import datetime
@anvil.server.callable
def get_history():
data = anvil.http.request('https://corona.lmao.ninja/v2/historical/south%20africa', json=True)
cases = data['timeline']['cases']
cases = {datetime.strptime(k,'%m/%d/%y').strftime('%Y-%m-%d'): v for k,v in cases.items()}
Client Side
from datetime import datetime
from collections import OrderedDict
cases = anvil.server.call('get_history')
cases = OrderedDict(sorted(cases.items(), key=lambda case: case[0]))
This gives dates as strings you can order
1 Like
Thank you for your assistance. Put me on the right track:
Did the below:
ServerModule:
def get_history():
resp_hist = anvil.http.request('https://corona.lmao.ninja/v2/historical/south%20africa',json=True)
cases = resp_hist['timeline']['cases']
cases = OrderedDict(sorted(cases.items(), key=lambda t: datetime.strptime(t[0], '%m/%d/%y')))
data = list(cases.items())
return data
Client Side:
history = anvil.server.call('get_history')
dates = []
values = []
for key in history:
dates.append(key[0])
values.append(key[1])
self.daily_cases_za_bar.data = [go.Scatter(x=dates, y=values)]
Works fine
1 Like
two lists are easier to plot in plotly so it’s probably the way to go anyway!
you could also do:
history = anvil.server.call('get_history')
dates = list(history.keys())
values = list(history.values())
1 Like
Sure your method is more pythonic way of getting it done.
Anyway your input changed the way I was thinking about the problem.
Many thanks.
1 Like
Python dictionary did become ordered in Python 3.6, but not really officially. Some Python 3.6+ implementations could still have non ordered dictionaries.
I still don’t rely on dictionaries being ordered.
1 Like