0

I am trying to create a dynamic table with flask by passing parameters through the redirect(url_for()) function and some of the variables are being passed but my data array is not showing on the redirected html page. I have a lot of imported libraries, as this is a large project and have a lot of other code written. I just think I am repeatedly skipping over the issue as I have just been looking at this for hours and cannot figure out why my html will not print the values I have got from yahoo finance's. It does print the variable for on the browser page, however. I used ".to_html()" on another python function and wrote to a file in my directory, but its very ugly and displays poorly on my browser route. I know it will not work for what I want to do.

Python Code:

from typing_extensions import final
from flask import Flask, redirect, url_for, render_template, request
from pandas.core.frame import DataFrame
import requests as req
import time 
import datetime
import os
import pandas as pd
import matplotlib.pyplot as plt
import statsmodels.api as sm
import numpy as np
import scipy.stats as st
from scipy.stats import truncnorm
from openpyxl import Workbook
import yfinance as yf
from yfinance import tickers
import json2html

class table_data_single:
    def __init__(self, PE, market_cap, EPS):
        self.PE = PE
        self.market_cap = market_cap
        self.EPS = EPS

@app.route("/home", methods=["POST","GET"])
def home():
    if request.method == "POST":
        stock = request.form["ticker"]
        tick = yf.Ticker(stock)
        PE = tick.info["forwardPE"]
        market_cap = tick.info["marketCap"]
        EPS = tick.info["forwardEps"]
        data = [(
            table_data_single(PE, market_cap, EPS)
        )
        ]
        return redirect(url_for('single_analysis', ticker=stock, info=data))
    else:
        return render_template('home.html')

@app.route("/home/single_analysis/<ticker>/<info>", methods=["POST", "GET"])
def single_analysis(info, ticker):
    return render_template('single_analysis.html', ticker=ticker, info=info)

HTML code:

{% extends "home.html" %}
{% block content %}
    <div class="single_analysis">
        <h3>Searched Ticker: {{ticker}}</h3>
        <h4>{{ticker}} Stock Metrics</h4>
        <table class="Ticker_Metrics">
            <tr class="ticker_metrics_headers">
                <th>Forward P/E</th>
                <th>Market Cap</th>
                <th>Forward EPS</th>
            </tr>
            <tbody>
                <tr class="table_row">
                    {% for i in info %}
                        <td>{{ i.PE }} {{ i.market_cap }} {{ i.EPS }} </td>
                    {% endfor %}
                </tr>
            </tbody>
        </table>
        <h4>{{ticker}} Balance Sheet Metrics</h4>
    </div>
{% endblock %}
  • *The variable is being passed and showing on my html page, but the data array is not showing up where I have it on my html page, it just shows nothing. – Brendan Lydon Jan 17 '22 at 16:24
  • cannot pass an object as a URL variable. look at this question for potential solution(s): https://stackoverflow.com/q/47624686/42346 – mechanical_meat Jan 17 '22 at 16:45
  • thank you I figured it out. I just had to pass the variable and perform the api calls in the single_analysis function. Huge Help! – Brendan Lydon Jan 17 '22 at 18:52

0 Answers0