Forums

web Dash app rendering issues when deployed

Hi all,

I have a Dash app that's based on the dash_bootstrap_components. More specifically, the code generates a collapsible side-bar. The code is adapted from here. When running the code locally, I don't have any problems and the page renders just fine:

(can't seem to get picture implementation to work, please have a look at the links)

https://ibb.co/Ny9jLHV

However, running the code on pythonanywhere, I get the following layout:

https://i.ibb.co/tc469fb/Untitled.jpg

For code see below.

I'd appreciate any input.

Cheers, M

My code looks as follows, split over 3 files:

-mretier_pythonanywhere_com_wsgi.py
 import sys
    project_home = u'/home/mretier/'
    if project_home not in sys.path:
        sys.path = [project_home] + sys.path
    sys.path.append(u'/home/mretier/app.py')
    from index import app
    application = app.server


-app.py
import dash
import dash_bootstrap_components as dbc


app = dash.Dash(__name__, suppress_callback_exceptions=True,
                external_stylesheets=[dbc.themes.BOOTSTRAP],
                # these meta_tags ensure content is scaled correctly on different devices
                # see: https://www.w3schools.com/css/css_rwd_viewport.asp for more
                meta_tags=[
                    {"name": "viewport", "content": "width=device-width, initial-scale=1"}
                ],
                )
server = app.server


-index.py 
# defines layout and responsiveness of sidebar taken from dash_bootstrap_components examples

import dash_bootstrap_components as dbc
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output, State

from app import app


# we use the Row and Col components to construct the sidebar header
# it consists of a title, and a toggle, the latter is hidden on large screens
sidebar_header = dbc.Row(
    [
        dbc.Col(html.H2("Sidebar", className="display-4")),
        dbc.Col(
            [
                html.Button(
                    # use the Bootstrap navbar-toggler classes to style
                    html.Span(className="navbar-toggler-icon"),
                    className="navbar-toggler",
                    # the navbar-toggler classes don't set color
                    style={
                        "color": "rgba(0,0,0,.5)",
                        "border-color": "rgba(0,0,0,.1)",
                    },
                    id="navbar-toggle",
                ),
                html.Button(
                    # use the Bootstrap navbar-toggler classes to style
                    html.Span(className="navbar-toggler-icon"),
                    className="navbar-toggler",
                    # the navbar-toggler classes don't set color
                    style={
                        "color": "rgba(0,0,0,.5)",
                        "border-color": "rgba(0,0,0,.1)",
                    },
                    id="sidebar-toggle",
                ),
            ],
            # the column containing the toggle will be only as wide as the
            # toggle, resulting in the toggle being right aligned
            width="auto",
            # vertically align the toggle in the center
            align="center",
        ),
    ]
)

sidebar = html.Div(
    [
        sidebar_header,
        # we wrap the horizontal rule and short blurb in a div that can be
        # hidden on a small screen
        html.Div(
            [
                html.Hr(),
                html.P(
                    "A responsive sidebar layout with collapsible navigation "
                    "links.",
                    className="lead",
                ),
            ],
            id="blurb",
        ),
        # use the Collapse component to animate hiding / revealing links
        dbc.Collapse(
            dbc.Nav(
                [
                    # dbc.NavLink("Page 1", href="/page-1", id="page-1-link"),
                    # dbc.NavLink("Page 2", href="/page-2", id="page-2-link"),
                    # dbc.NavLink("Page 3", href="/page-3", id="page-3-link"),
                    dcc.Link("Page 1", href="/page-1", id="page-1-link"),
                    dcc.Link("Page 2", href="/page-2", id="page-2-link"),
                    dcc.Link("Page 3", href="/page-3", id="page-3-link"),

                ],
                vertical=True,
                pills=True,
            ),
            id="collapse",
        ),
    ],
    id="sidebar",
)

content = html.Div(id="page-content")

app.layout = html.Div([dcc.Location(id="url"), sidebar, content])


# this callback uses the current pathname to set the active state of the
# corresponding nav link to true, allowing users to tell see page they are on
@app.callback(
    [Output(f"page-{i}-link", "active") for i in range(1, 4)],
    [Input("url", "pathname")],
)
def toggle_active_links(pathname):
    if pathname == "/":
        # Treat page 1 as the homepage / index
        return True, False, False
    return [pathname == f"/page-{i}" for i in range(1, 4)]


@app.callback(Output("page-content", "children"), [Input("url", "pathname")])
def render_page_content(pathname):

    if pathname in ["/", "/page-1"]:
        return html.P("This is the content of page 1!")
        # return page_delmaker.page_delmaker_layout
    elif pathname == "/page-2":
        return html.P("This is the content of page 2. Yay!")
    elif pathname == "/page-3":
        return html.P("Oh cool, this is page 3!")
    # If the user tries to reach a different page, return a 404 message
    return dbc.Jumbotron(
        [
            html.H1("404: Not found", className="text-danger"),
            html.Hr(),
            html.P(f"The pathname {pathname} was not recognised..."),
        ]
    )


@app.callback(
    Output("sidebar", "className"),
    [Input("sidebar-toggle", "n_clicks")],
    [State("sidebar", "className")],
)
def toggle_classname(n, classname):
    if n and classname == "":
        return "collapsed"
    return ""


@app.callback(
    Output("collapse", "is_open"),
    [Input("navbar-toggle", "n_clicks")],
    [State("collapse", "is_open")],
)
def toggle_collapse(n, is_open):
    if n:
        return not is_open
    return is_open


if __name__ == "__main__":
    app.run_server(port=8888, debug=True)

It looks like some of your static files are failing to load. Do you see any errors in your browser's developer console when you load your page? In Chrome, you can access the console from the "hamburger" menu, then "More tools", then "Developer tools".

Thanks for the hint. My 'assets' folder with the .css sheet was not properly uploaded to the pythonanywhere environment and I didn't think about it since this doesn't return any errors.