Dazed and confused...show plot

I have been working with Anvil for about 1 week and I am extremely confused by the documentation. I am making an api call via the server module and am able to print the return but for the life of me I cannot understand the process of getting a plot to show. Maybe something simple but I find the docs to be really vague at times and they seem to skip allot of details…

This is my form code:


from ._anvil_designer import Form2Template
from anvil import *
import plotly.graph_objects as go
import anvil.server
import json

class Form2(Form2Template):
  def __init__(self, **properties):
    # Set Form properties and Data Bindings.
    self.init_components(**properties)

    # Any code you write here will run before the form opens.
    stock_data = anvil.server.call('get_stock_history_tradier', 'aapl', 'daily', 'month')
    

    print(stock_data)
    
    

    self.plot_1.data = go.Scatter(x =[p['history']['day']['date'] for p in stock_data],
                                 y = [p['history']['day']['close'] for p in stock_data],
                                 fill = 'tozeroy')

Clone link:
share a copy of your app

this is my api print;


{'history': {'day': [{'close': 150.87, 'date': '2023-02-09', 'high': 154.33, 'low': 150.42, 'open': 153.775, 'volume': 56007143}, {'close': 151.01, 'date': '2023-02-10', 'high': 151.3401, 'low': 149.22, 'open': 149.46, 'volume': 57450708}, ......

this is my error message
TypeError: string indices must be integers, not str

  • at Form2, line 22

I have tried many possible combos but I am missing something

Looks like your loops should be more like


    print(stock_data)
    day_data = stock_data['history']['day']
    print(day_data)
    

    self.plot_1.data = go.Scatter(x =[p['date'] for p in day_data],
                                 y = [p['close'] for p in day_data],
                                 fill = 'tozeroy')


to see the issue in your original code try iterating in a loop and printing the value at each iteration


for p in stock_data:
    print(p)

stock_data is a dictionary so you were iterating over the keys. The first of which is "history".
Doing "history"['history']['day'] doesn’t make much sense.

stock_data['history']['day'] on the other hand gets you a list of day data.


for p in day_data:
    print(p) # {'close': 150.87, 'date': '2023-02-09', 'high': 154.33, 'low': 150.42, 'open': 153.775, 'volume': 56007143}

2 Likes

Thank you so much. I appreciate your time. That worked for the graphing. I have another newby question. Do I need to iterate thru the data in order to show on a repeating panel? Docs seem to indicate that it handles it automatically but can’t seem to get that to work either so I am wondering if thats my issue. Thanks again.

You need to pass an iterable array (dicts, tuples, etc.) to a repeating panel’s items property, so in your case based on the info above :

self.repeating_panel_1.items = stock_data['history']['day']

You’ll need to ensure you have bound some labels/whatever to the data you are passing (the docs explain this well - Anvil Docs | RepeatingPanels).

1 Like