Dynamic dbc.Links from text file

Hello, i have a dash app which i need to have dynamic dbc.Links on some section.
So i’m looking for a way to import from text file the links (like initial configuration file).

For example instead of having:

        dbc.Row('Google Link') ,
        dbc.Row(dcc.Link(html.A('Google'),  href='www.google.com',  refresh=True)),

i would like to import the above link from text file using :
f = open(‘links.txt’, “r”)
txtlines = f.read().splitlines()
f.close()

Is there any way to construct that text and evaluate in dash layout normally ?
Thanks for any suggestions.

To achieve dynamic generation of dbc.Row and dcc.Link components in your Dash app based on the contents of a text file, you can follow these steps. The approach involves reading the link information from a text file, parsing it, and then programmatically creating the components based on that data.

Suppose your links.txt file contains lines formatted like this for each link:

arduinoCopy code

Google|https://www.google.com
Another Site|https://www.anothersite.com

Each line represents a link with the display text and URL separated by a |. Here’s how you can read this file and generate the components:

Step 1: Read the Links from the File

Read the file and split it into lines, then further split each line into display text and URL:

pythonCopy code

# Open the file and read the lines
with open('links.txt', "r") as f:
    txtlines = f.read().splitlines()

# Split each line into text and href
links = [line.split('|') for line in txtlines]

Step 2: Generate the Components

Loop through the parsed links to create dbc.Row and dcc.Link components dynamically:

pythonCopy code

import dash_core_components as dcc
import dash_bootstrap_components as dbc
import dash_html_components as html

# Function to generate a list of rows with links
def generate_link_rows(links):
    rows = []
    for text, href in links:
        link_component = dbc.Row(
            dcc.Link(html.A(text), href=href, refresh=True)
        )
        rows.append(link_component)
    return rows

Step 3: Use in Your Dash Layout

Use the generate_link_rows function to add the dynamically created rows to your layout:

pythonCopy code

app.layout = html.Div([
    # Other components...
    *generate_link_rows(links),
    # More components...
])

By using the * operator, you’re unpacking the list of components returned by generate_link_rows directly into the layout.

Complete Example

Combining everything into a simple Dash app:

pythonCopy code

import dash
import dash_core_components as dcc
import dash_bootstrap_components as dbc
import dash_html_components as html

# Read links from file
with open('links.txt', "r") as f:
    txtlines = f.read().splitlines()
links = [line.split('|') for line in txtlines]

# Generate link components
def generate_link_rows(links):
    rows = []
    for text, href in links:
        link_component = dbc.Row(
            dcc.Link(html.A(text), href=href, refresh=True)
        )
        rows.append(link_component)
    return rows

# Initialize the Dash app (assuming you're using Dash with Bootstrap)
app = dash.Dash(__name__, external_stylesheets=[dbc.themes.BOOTSTRAP])

# Setup the layout
app.layout = html.Div([
    *generate_link_rows(links),
])

if __name__ == '__main__':
    app.run_server(debug=True)

Ensure your links.txt file is in the correct format and the path to the file is correct relative to where your script is run. This approach provides a flexible way to manage links externally and can be extended or modified to fit more complex requirements or different file formats.

2 Likes

Thank you very much for your example.
Very useful new thing for me is the * operator that unpacks the list as multiple dbc.Row.