I am trying to run this simple code which fetches the price for 3 symbols.
i get the values fine from server and am able to display single values in labels fine. however i am unable to bind it to Repeating panel. all videos i watched on repeating panel show a step to attach them to datatables. in this case, i am not storing anything and there is no data table. how do i directly bind a JSON list to a label box within a repeating panel ?
def auth_successful(self, **event_args):
self.label_2.text = âAuth successful!â
quotes = anvil.server.call(âget_quotesâ,âAAPL,CTSH,NDAQâ)
for iterator in quotes:
self.symbol.text = (quotes[iterator][âsymbolâ])
self.price.text = (quotes[iterator][âclosePriceâ])
output
AAPL 134.87
CTSH 54.02
NDAQ 63.61
edit: i followed the tutorial for data grid binding and per that i added the following, however it throws an error that RowTemplate1 is not defined. i added data grid, defined 2 columns and then under init added the following line
self.repeating_panel_1.items = anvil.server.call(âget_quotesâ,âAAPL,CTSH,NDAQâ)
You donât show the server function, but that for loop is a loop over a dictionary, not a list. A data grid/repeating panel must be given a list of dictionaries.
Maybe show the code for the server function itself? Be sure to put the code between three backticks so the forum formats it as Python code.
Youâre calling an external API to get some results, and then passing those results back to the client. What does a typical response look like? If you do print (r) after you convert the results from JSON, what gets printed?
print (r) just shows me : <Response [200]>
if i do r = r.json() and then print (r), it looks like this
{
âAAPLâ:{
âassetTypeâ:âEQUITYâ,
âsymbolâ:âAAPLâ,
âclosePriceâ:146.87
},
âCTSHâ:{
âassetTypeâ:âEQUITYâ,
âsymbolâ:âCTSHâ,
âclosePriceâ:57.29
},
âNDAQâ:{
âassetTypeâ:âEQUITYâ,
âassetMainTypeâ:âEQUITYâ,
âsymbolâ:âNDAQâ,
âclosePriceâ:66.21
}
}
Thatâs a dictionary of dictionaries, not a list of dictionaries. You canât pass that to a repeating panel without converting it into a list of dictionaries.
The problem in this case is what to do with the keys. In the SO question, the keys arenât required, so theyâre just discarded. Is that the case here or are they also needed?
I have not either, but if r.values() does not work directly, then passing (x for x in r.values() ) definitely will.
(using a generator comprehension instead of turning it into a list in memory first)
i am getting the correct output using these:
dict = json.loads(r.text)
list_of_dics = [value for value in dict.values()]
return (list_of_dics)
however when i drag-n-drop repeating panel and bind to it
self.repeating_panel_1.items=anvil.server.call(âget_quotesâ,âAAPL,CTSH,NDAQâ)
i keep getting the error
NameError: name âItemTemplate1Templateâ is not defined
in the Tutorials i see this ItemTemplate get created as a child of main form, but for me it gets created the the root level, not as a child of Main. i did set the Data Bindings to self.item[âsymbolâ]
If you click on the repeating panel and look at its properties, thereâs a place to say which item template to use:
That should automatically get hooked up to the new item template form when you drag a repeating panel onto your form. Look at that section and see what yours looks like.