title: Rendering multiple dataframes from API calls in Django Views
body: <p>I have a page that is meant to show company financials based on customers input(they input which company they're looking for). Once they submit, in the view function I want to create 5 API urls which then get the json data, create a list with the dates from 1 API result (they will all contain the same dates, so I will use the same list for all), then create new dictionaries with specific data from each api call, as well as the list of dates. I then want to create dataframes for each dictionary, then render each as html to the template.</p>
  <p>My first attempt at this I attempted to do all requests.get and jsons.load calls one after another in a try block within &quot; if request.method == 'POST' &quot; block. This worked well when only grabbing data from one API call, but did not work with 5. I would get the local variable referenced before assigned error, which makes me think either the multiple requests.get or json.loads was creating the error.</p>
  <p>My current attempt(which was created out of curiosity to see if it worked this way) does work as expected, but is obv not correct as it is calling the API multiple times in the for loop, as shown. (I have taken out some code for simplicity)</p>
  <pre><code>def get_financials(request, *args, **kwargs):
      pd.options.display.float_format = '{:,.0f}'.format
      IS_financials = {} #Income statement dictionary
      BS_financials = {} #Balance sheet dictionary
      dates = []
  
      if request.method == 'POST':
          ticker = request.POST['ticker']
          IS_Url = APIURL1
          BS_URL = APIURL2
          
          try:
              IS_r = requests.get(IS_Url)
              IS = json.loads(IS_r.content)       
      
              for year in IS:
                  y = year['date']
                  dates.append(y)
              
              for item in range(len(dates)):
                  IS_financials[dates[item]] = {}
                  IS_financials[dates[item]]['Revenue'] = IS[item]['revenue'] / thousands
                  IS_financials[dates[item]][&quot;Cost of Revenue&quot;] = IS[item]['costOfRevenue'] / thousands
                  IS_fundementals = pd.DataFrame.from_dict(IS_financials, orient=&quot;columns&quot;)
  
              for item in range(len(dates)):
                  BS_r = requests.get(BS_URL)
                  BS = json.loads(BS_r.content)
                  BS_financials[dates[item]] = {}
                  BS_financials[dates[item]]['Cash and Equivalents'] = BS[item]['cashAndCashEquivalents'] / thousands
                  BS_financials[dates[item]]['Short Term Investments'] = BS[item]['shortTermInvestments'] / thousands
                  BS_fundementals = pd.DataFrame.from_dict(BS_financials, orient=&quot;columns&quot;)
                  
  
          except Exception as e:
              apiList = &quot;Error...&quot;
  
          return render(request, 'financials.html', {'IS': IS_fundementals.to_html(), 'BS': BS_fundementals.to_html()})
      else:
          return render(request, 'financials.html', {})
  </code></pre>
  <p>I'm trying to think of the proper way to do this. I'm new to django/python and not quite sure the best practice for a problem like this would be. I thought about making separate functions for each API, but then I would be unable to render them all on the same page. Can I use nested functions? Where only the main function renders to template, and all inner functions simply return the dataframe to outer function? Would class based views be better for something like this? I have never worked with class based views yet so would be a bit of a learning curve.</p>
  <p>Another question I have is how to change the html in the table that is rendered from dataframe? The table/font that is currently rendered is quite large.</p>
  <p>Thanks for any tips/advice!</p>