# Raw html as value of component

**URL:** <https://community.plotly.com/t/raw-html-as-value-of-component/7250>\
**Category:** Dash Python\
**Created:** [December 11, 2017, 3:33am UTC](https://community.plotly.com/t/raw-html-as-value-of-component/7250 "2017-12-11T03:33:51Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![bkelemen](https://avatars.discourse-cdn.com/v4/letter/b/5fc32e/32.png) [@bkelemen](https://community.plotly.com/u/bkelemen)\
**Post date:** [December 11, 2017, 3:33am UTC](https://community.plotly.com/t/raw-html-as-value-of-component/7250/1 "2017-12-11T03:33:51Z")

</div>

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:

```auto
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.

---

<div class="post-metadata">

**Author:** ![chriddyp](https://sea2.discourse-cdn.com/flex024/user_avatar/community.plotly.com/chriddyp/32/16855_2.png) [@chriddyp](https://community.plotly.com/u/chriddyp)\
**Post date:** [December 11, 2017, 10:54pm UTC](https://community.plotly.com/t/raw-html-as-value-of-component/7250/2 "2017-12-11T22:54:37Z")

</div>

> [@bkelemen](#):
>
> While this works for this simple use-case, there can be others cases where simple raw html may be useful.

Sometimes the `dcc.Markdown` component can be helpful in the text is simple (e.g. new lines). As a last resort, you can use the `dash-dangerously-set-inner-html` component to render raw html [GitHub - plotly/dash-dangerously-set-inner-html: Dash component to dangerously set inner raw HTML](https://github.com/plotly/dash-dangerously-set-inner-html)

---

<div class="post-metadata">

**Author:** ![gaw89](https://avatars.discourse-cdn.com/v4/letter/g/eb9ed0/32.png) [@gaw89](https://community.plotly.com/u/gaw89)\
**Post date:** [August 31, 2018, 1:33pm UTC](https://community.plotly.com/t/raw-html-as-value-of-component/7250/3 "2018-08-31T13:33:49Z")

</div>

Is there a way to add a callback to an element created by DangerouslySetInnerHTML?

---

<div class="post-metadata">

**Author:** ![chriddyp](https://sea2.discourse-cdn.com/flex024/user_avatar/community.plotly.com/chriddyp/32/16855_2.png) [@chriddyp](https://community.plotly.com/u/chriddyp)\
**Post date:** [August 31, 2018, 1:49pm UTC](https://community.plotly.com/t/raw-html-as-value-of-component/7250/4 "2018-08-31T13:49:01Z")

</div>

> [@gaw89](#):
>
> Is there a way to add a callback to an element created by DangerouslySetInnerHTML?

There is not, only the top level properties of dash components can be inputs of callbacks

---

<div class="post-metadata">

**Author:** ![gaw89](https://avatars.discourse-cdn.com/v4/letter/g/eb9ed0/32.png) [@gaw89](https://community.plotly.com/u/gaw89)\
**Post date:** [August 31, 2018, 2:39pm UTC](https://community.plotly.com/t/raw-html-as-value-of-component/7250/5 "2018-08-31T14:39:07Z")

</div>

Thanks for the quick reply!

The reason I was asking is that I am trying to use Materialize CSS framework with Dash. For checkboxes, it needs the label text to be inside a span as seen below:

```
<label class="" style="display: inline-block; margin-right: 8px;">
    <input type="checkbox" class="" value="on">
        <span>
            <!-- react-text: 62 -->Checkbox Label<!-- /react-text --> 
        </span>
</label>

```

However, the HTML that is generated by Dash using dcc.Checklist is (without the span):

```
<label class="" style="display: inline-block; margin-right: 8px;">
    <input type="checkbox" class="" value="on">
        <!-- react-text: 62 -->Checkbox Label<!-- /react-text --> 
</label>

```

Is there a way to get the label text to be inside a span? I could make a very hacky solution with some custom JS, but is there a better way?

Thanks!

---

<div class="post-metadata">

**Author:** ![chriddyp](https://sea2.discourse-cdn.com/flex024/user_avatar/community.plotly.com/chriddyp/32/16855_2.png) [@chriddyp](https://community.plotly.com/u/chriddyp)\
**Post date:** [August 31, 2018, 8:46pm UTC](https://community.plotly.com/t/raw-html-as-value-of-component/7250/6 "2018-08-31T20:46:25Z")

</div>

> [@gaw89](#):
>
> Is there a way to get the label text to be inside a span? I could make a very hacky solution with some custom JS, but is there a better way?

You could write your own checklist component: [Build Your Own Components | Dash for Python Documentation | Plotly](https://dash.plot.ly/plugins) and pull out the code from the open source Checklist
