0

Just created my first fast api interface. Quite simple. I want to display a list of rows on the page.

python code is:


## ----------------------------------------------------------------------------------
## Collect data
## ----------------------------------------------------------------------------------

idb = sqle.db_engine(tg_db_name, data_path)
idb.connect()
rows = idb.ticker_list()
idb.close_db()

## ----------------------------------------------------------------------------------
## Main
## ----------------------------------------------------------------------------------

templates = Jinja2Templates(directory="templates")

app = FastAPI()

@app.get("/")

def index(request: Request):
    # print(dir(request))
    # return{"Hello":"DashBoard", "Stocks" : rows}
    return templates.TemplateResponse("index.html", {"request": request, "Stocks": rows})

when not commented, the following line displays the rows information information

   return{"Hello":"DashBoard", "Stocks" : rows}

rows displayed

when not commented, this one just returns an empty page. Just "Stocks" is displayed at the top.

     return templates.TemplateResponse("index.html", {"request": request, "Stocks": rows})

Not displayed

here below the index.html template

<html>
    <head>
        <tile>Stocks</title>
    </head>
    <body>
        <table>
        {% for row in rows %}
            {{ rows.id }}
        {% endfor %}
        </table>
    </body>
</html>

also, row type is <class 'list'>

and it is the retreival of an sqllite query.

   mySql = ('''SELECT id_ticker, tx_ticker_symbol, tx_ticker_code, 
               tx_ticker_name, tx_ticker_fullname 
              FROM Ticker''')
    self.cursor.execute(mySql) 
    rows = self.cursor.fetchall()
    
    return rows

I would expect the following line to display the rows information.

     return templates.TemplateResponse("index.html", {"request": request, "Stocks": rows})

Also, I don't understand why rows is return as a list and not a dictionary.

gfx Prog
  • 21
  • 2
  • What do you expect `{% for row in rows %}` to do? You're assigning your rows under the name `Stocks`: `"Stocks": rows` - so you'll have to refer to it by that name: `{% for stock in Stocks %} {{ stock.id }} {% endfor %}` – MatsLindh Jan 26 '23 at 22:23
  • thks. I do understand now. – gfx Prog Jan 26 '23 at 22:47
  • any reason why i get a list rather than a dictionnary when using self.cursor.fetchall() ? – gfx Prog Jan 26 '23 at 22:48
  • `fetchall` means to fetch _all rows_, so that needs to be a list? A list of rows. If you want the _elements_ of the list to be dictionaries, that support might depend on your library, but for `mysql-connector-python`, you can use `connection.cursor(dictionary=True)` to get a cursor that returns rows as dictionaries. – MatsLindh Jan 27 '23 at 11:24
  • Does this answer your question? [How to navigate through FastAPI routes by clicking on HTML button in Jinja2 Templates?](https://stackoverflow.com/questions/74237542/how-to-navigate-through-fastapi-routes-by-clicking-on-html-button-in-jinja2-temp) – Chris Jan 27 '23 at 13:28
  • thks to all ! working quite well so far – gfx Prog Feb 02 '23 at 15:10

0 Answers0