question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Rendering raw HTML

See original GitHub issue

Thanks for Dash! 😄

I am trying to pass raw HTML through to Dash, such as HTML returned by calling pd.DataFrame.to_html() (as a shortcut to defining a function such as generate_table() in the getting started docs).

React seems to support this through a div that sets dangerouslySetInnerHTML. Passing this to html.Div raises an exception that the argument is not one of the 14 that are pre-defined.

Is there any way to do this with Dash?

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:3
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

6reactions
TheoLvscommented, Jan 10, 2018

Hello guys, I stumbled on the same issue recently, I managed to solve it with Beautifulsoup and a recursive function

Parse your html with BeautifulSoup :

import bs4 as bs
html_parsed = bs.BeautifulSoup(html_snippet)

And use the following recursive function to convert the parsed html to a set of dash-html-components

import dash_html_components as html

def convert_html_to_dash(el,style = None):
    if type(el) == bs.element.NavigableString:
        return str(el)
    else:
        name = el.name
        style = extract_style(el) if style is None else style
        contents = [convert_html_to_dash(x) for x in el.contents]
        return getattr(html,name.title())(contents,style = style)

With the helper function to get a style dictionary from a list of style arguments of a Beautifulsoup element

def extract_style(el):
    return {k.strip():v.strip() for k,v in [x.split(": ") for x in el.attrs["style"].split(";")]}

This was a quick hack, but worked perfectly for what I wanted to do !

6reactions
chriddypcommented, Dec 7, 2017

@frogger72 - Good point. I ran into this myself on a personal project where I wanted to render HTML from a third-party source and converting it to the dash-html-components library would’ve been too complex. I have created a dangeroulsySetInnerHTML component that you can use now https://github.com/plotly/dash-dangerously-set-inner-html. However, I recommend using the dash-html-components as much as possible.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Rendering raw html with reactjs - javascript - Stack Overflow
Use built-in APIs to parse a raw HTML string into an HTML Element; Recursively transform an Element object (and its children) into ReactElement ......
Read more >
Render Raw HTML In React | Become Front-End Expert
Learn how to sanitize and render raw HTML code in React using the dangerouslySetInnerHTML prop or external libraries.
Read more >
Render raw HTML example - CodeSandbox
CodeSandbox is an online editor tailored for web applications.
Read more >
React Render Raw Plain HTML String - CodingShower
Let's explore all the different ways to render raw HTML strings in React. dangerouslySetInnerHTML. Using the dangerouslySetInnerHTML ...
Read more >
How do I render raw HTML in Blazor? - Syncfusion
Raw HTML can be rendered in Blazor by using the MarkupString. You can set the raw HTML as a string to any parameter...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found