Hello,
Is it possible to easily insert content (children of a dash component) with raw html embedded?
I’m trying to insert raw html into a <td>
html component. In particular, my use-case is to display text that has embedded '\n'
characters as paragraph separators.
I tried to replace \n
with <p></p>
with no luck, and was only able to implement by replacing the text with the dash components required to split the sentences and intersperse with html.P
's. While this works for this simple use-case, there can be others cases where simple raw html may be useful.
This example below shows the case of: (1) text with \n
embedded, (2) text with \n
replaced with <p></p>
, and (3) [work around] converting the text into dash components:
from more_itertools import intersperse
import dash
from dash.dependencies import Input, Output
import dash_core_components as dcc
import dash_html_components as html
app = dash.Dash()
app.css.append_css({
'external_url': 'https://codepen.io/chriddyp/pen/dZVMbK.css'
})
app.layout = html.Div([
html.Button('Submit', id='button'),
html.Div(id='output')
])
content1 = 'This is sentence 1.\nAnd this is sentence two.\nFinally three.'
content2 = 'This is sentence 1.<p></p>And this is sentence two.<p></p>Finally three.'
def make_content_with_paragraphs(content):
cwp = html.Div(list(intersperse(html.P(), content.split('\n'))))
return cwp
@app.callback(
Output('output', 'children'),
[Input('button', 'n_clicks')])
def output(n_clicks):
if n_clicks % 3 == 0:
return 'Example 1: (embedded "\\n"): ' + content1
elif n_clicks % 3 == 1:
return 'Example 2: (embedded "<p></p>"): ' + content2
else:
return html.Div(['Example 3: (converting to dash components): ',
make_content_with_paragraphs(content1)])
if __name__ == '__main__':
app.run_server(debug=True)
Thanks.