0

Let's say I have 10 records on my database and I want to render all of them in my Dash web app, is there a way where I can render all of them through a for loop? I've tried creating a function that renders the element but when I try to iterate through the records it only renders the first one.

x = ['Tom', 'Jerry', 'Math']

def createRegister(name):
    return dbc.Col([
       html.P(f"{name}")
    ])

app.layout = dbc.Container([
    dcc.Interval(id='interval_db', interval=86400000 * 7, n_intervals=0),
    dbc.Row([

    ], id='row-teste')
], fluid=True)

@app.callback(
    Output('row-teste', 'children'),
    [Input('interval_db', 'n_intervals')]
)

def p(n_intervals):
    for i in x:
        return createReg(i)

What am I doing wrong here?

1 Answer 1

0

You could modify createRegister to take a list of records as the input, and return multiple html.P components using a list comprehension:

import dash
from dash import dcc, html, Input, Output
import dash_bootstrap_components as dbc

# Initialize the dash app
app = dash.Dash()

x = ['Tom', 'Jerry', 'Math']

def createRegister(names):
    return dbc.Col([
       html.P(f"{name}") for name in names
    ])

app.layout = dbc.Container([
    dcc.Interval(id='interval_db', interval=1000, n_intervals=0),
    dbc.Row([

    ], id='row-teste')
], fluid=True)

@app.callback(
    Output('row-teste', 'children'),
    [Input('interval_db', 'n_intervals')]
)

def p(n_intervals):
    return createRegister(x)

app.run_server(debug=True)

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.