Dash – Include custom html object

Question:

I’m creating a Dash application in Python to showcase results of some Topic Analysis I performed. For topic analysis there is a nice visualisation tool called pyLDAvis. I used this tool, and saved its output as a html file named lda.html:

# Visualisatie
topic_data =  pyLDAvis.gensim.prepare(ldamodel, doc_term_matrix, dictionary, mds = "mmds")#mds = 'pcoa')
pyLDAvis.save_html(topic_data, 'lda.html')
pyLDAvis.display(topic_data)

My current Dash application includes a table that can be filtered on multiple topics. Underneath this table I want to present lda.html. The below code contains some of the attempts I have done

#import os

#STATIC_PATH = os.path.join(os.path.dirname(os.path.abspath('lda.html')), 'static')
#STATIC_PATH
import dash
import dash_html_components as html
import dash_core_components as dcc
import plotly.graph_objects as go
import plotly.express as px
import dash_dangerously_set_inner_html
from dash import dash_table
from dash.dependencies import Input
from dash.dependencies import Output

app = dash.Dash()

topics = df_topics_wegschrijven['Topic'].unique().tolist()
                                  
app.layout = html.Div(
    children=[
        dcc.Dropdown(
            id="filter_dropdown",
            options=[{"label": tp, "value": tp} for tp in topics],
            placeholder="Selecteer een topic",
            multi=True,
            value=df_topics_wegschrijven.Topic.unique(),
        ),
        dash_table.DataTable(id = "table-container", 
                             data = df_topics_wegschrijven.to_dict('records'), 
                             columns =  [{"name": i, "id": i} for i in df_topics_wegschrijven.columns],
                            ),
        #html.Iframe(src='/static/lda.hmtl'), #style=dict(position="absolute", left="0", top="0", width="100%", height="100%"))
        html.Iframe(src=r"C:UsersMyUserNameDocumentslda.html")
        #html.Iframe(topic_data)
    ]
)  

@app.callback(
    Output("table-container", "data"), 
    Input("filter_dropdown", "value")
)
def display_table(topic):
    dff = df[df_topics_wegschrijven.Topic.isin(topic)]
    return dff.to_dict("records")


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

This code outputs the following:
enter image description here

As you can see there is an empty white square, where I would expect my lda.hmtl to be. For the code I commented out, the results are:

  1. html.Iframe(src=’/static/lda.hmtl’) -> The white square is now filled with ‘Not Found
    The requested URL was not found on the server. If you entered the URL manually please check your spelling and try again.’
  2. html.Iframe(topic_data) -> the entire dash application turns into ‘Error loading layout’.

To me it seems like what I did in my uncommented code should be correct (i.e. there is no error feedback except the square being blank), but I don’t understand why it returns a blank square.

When I for instance try

import webbrowser
  
webbrowser.open_new_tab('lda.html')

The visualisation loads as intended. I just cannot get it to work within my Dash application.

Does anyone have suggestions on how I can resolve my problem and load the pyLDAvis html file into Dash correctly?

Asked By: Coinman

||

Answers:

You’ve written the file extension as .hmtl instead of .html. That is probably the cause of the first problem.

UPDATE

I noticed that you’ve put lda.html into the static folder. In Dash, assets folder is used to store external resources.

html.Iframe(src='assets/lda.html')

Or in a more pythonic way

html.Iframe(src=app.get_asset_url('lda.html'))
Answered By: yapla

Is there any way to locate the reference html file outside of the asset folder and also outside the Dash app, for example when the html file is located in another folder or on another drive? In other words, how can this work? When I reference the file in this way, it is not displayed at all and no error message (no callback error etc.)

html.Iframe(src=r"C:UsersMyUserNameDocumentslda.html")
Answered By: dsforecast
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.