Black Lives Matter. Please consider donating to Black Girls Code today.

Way to build one set of html code and reuse multiple time

I am new to Dash and Python, so I am not sure if I am asking this question in the right way.

In some other programming languages, I could build a function, a stored proc, or another file of HTML code. This code then be loaded into a bigger file or web page.

For example, if I had a static section at the bottom of a web page, like the section at the bottom that always has copyright statement, a contact ling etc., I could put all of the html code into a separate function, a stored proc, or another file. Then on every web page, I could include this, and when the web page was executed, the code in my separate “container” would be placed into the web page and executed as if I had typed it out manually.

A simple example would be that if this is my code:

html.Div([
html.H1(‘Sample Title’),
html.P(‘Sample Text’)
])

I would like to do something like this

variable = html.Div([
html.H1(‘Sample Title’),
html.P(‘Sample Text’)
])

Then use the variable to insert the code in the appropriate spot on multiple pages.

Is there a way to do something similar in Dash Plotly?

I have looked at a def statement, and putting the code into a function. I am not sure how to "execute’ this function, if that is the way to do this.

Any help would be appreciated.

Hi @Jfrick100 , it looks like you’re looking for ways to re-use code in Python. If you’re using a function, your function should return objects (variables) which you can use elsewhere. You can also define variables and functions in other python files (modules) which you import (like when you import a 3rd party package, but here with you own modules). See for example https://scipy-lectures.org/intro/language/reusing_code.html

Emmanuelle,

I may have asked this incorrectly. I do know about modules, functions etc. and can import them and use them in other .py files. I also appreciate the link you sent. It covered a few areas I did not know about.

This is not my challenge. My challenge is how to put code like this:

html.Div([
html.P(‘Navigation’),
html.A(‘Dashapp 1’, href=’/dashapp1/’),html.P(’’),
html.A(‘Dashapp 2’, href=’/dashapp2/’),html.P(’’),
html.A(‘Dashapp 3’, href=’/dashapp3/’),html.P(’’),
html.A(‘Dashapp 4’, href=’/dashapp4/’),html.P(’’),
html.A(‘Dashapp 5’, href=’/dashapp5/’),html.P(’’),
html.A(‘Dashapp 6’, href=’/dashapp6/’),html.P(’’),
html.A(‘Dashapp 7’, href=’/dashapp7/’),html.P(’’),
html.A(‘Dashapp 8’, href=’/dashapp8/’),html.P(’’),
html.A(‘Dashapp 9’, href=’/dashapp9/’),html.P(’’)
])

into one .py file, import it into other .py files, and have it execute and give me a very simple list of links.

I just can’t seem to get it into a function, variable, or list and then execute so that there will be 9 links on my web pages.

I am able to make a list of 9 elements, one for each link. But I am unable to find a way to display the links on a web page in an automatic fashion, meaning if I add a 10th, 11th, etc. link,that it will show up correctly.

I appreciate your help on this.

James

I figured it out.

  1. define a method, for example:

def create_links():
return [html.A(‘Dashapp 1’, href=’/dashapp1/’),html.P(’’),
html.A(‘Dashapp 2’, href=’/dashapp2/’),html.P(’’),
html.A(‘Dashapp 3’, href=’/dashapp3/’),html.P(’’),
html.A(‘Dashapp 4’, href=’/dashapp4/’),html.P(’’),
html.A(‘Dashapp 5’, href=’/dashapp5/’),html.P(’’),
html.A(‘Dashapp 6’, href=’/dashapp6/’),html.P(’’),
html.A(‘Dashapp 7’, href=’/dashapp7/’),html.P(’’),
html.A(‘Dashapp 8’, href=’/dashapp8/’),html.P(’’),
html.A(‘Dashapp 9’, href=’/dashapp9/’),html.P(’’)]

Notice that the problem I had was trying to put in a line of html.A’s that are followed by a comma. This was the hangup.

To cure this, put all of the code inside a [ and ]

  1. to call this, use this code:

html.Div(children=create_links()
,
style={‘vertical-align’: ‘top’, ‘width’: ‘10%’, ‘display’: ‘inline-block’, ‘padding’: ‘10 10 20 20’})

Notice that in a normal html.Div([ …]), there are two brackets. In my code the are supplied by the create_links().

  1. The last thing to do is, of course, move the def create_links() to another file, then add “import” statement to bring it in from the other file.

Thanks for responding. I appreciate your time and help.

James