cancel
Showing results forย 
Search instead forย 
Did you mean:ย 
Data Engineering
Join discussions on data engineering best practices, architectures, and optimization strategies within the Databricks Community. Exchange insights and solutions with fellow data engineers.
cancel
Showing results forย 
Search instead forย 
Did you mean:ย 

displayHTML can't seem to be used from Python code, only hand typed into a cell???

T_1
New Contributor III

Trying to use displayHTML from w/in a Python module gets a Python exception:

NameError: name 'displayHTML' is not defined

and I've found no way around this. It seems to be something at the UI layer or something, not a Python function that can be referenced/imported or accessed/used from w/in code. Other notebooks provide methods for rendering HTML from w/in Python modules running in the notebook. The Ipython display/HTML methods don't work. Is there a Python function that will render HTML?

Here are a couple external issue I found:

https://stackoverflow.com/questions/71474139/how-to-import-displayhtml-in-databricks

https://github.com/sassoftware/saspy/issues/462

Any insight would be appreciated.

Thanks,

Tom

1 ACCEPTED SOLUTION

Accepted Solutions

ptweir
New Contributor III

Looks like this works!

from IPython.display import HTML, display
from IPython import get_ipython
try:
   shell = get_ipython().__class__.__name__
   if shell == 'DatabricksShell':
       display(HTML(html_string))

kudos to this stackoverflow answer: https://stackoverflow.com/questions/71474139/how-to-import-displayhtml-in-databricks

 

View solution in original post

13 REPLIES 13

Prabakar
Esteemed Contributor III

T_1
New Contributor III

Hey, thanks for reaching out. Unfortunately no, those don't help at all. I know the displayHTML call is the way a user renders HTML themselves by typing it into a cell. The problem is that that function doesn't exist in Python such that it can be executed by imported modules running in that environment.

I support a module users use which can provide results that are returned as HTML and, in other notebooks, rendered using the appropriate function for those environments, from my code; the user doesn't have to get the HTML object and manually render it themselves. That doesn't work in this notebook.

Trying to call that from w/in a module results in a NameError exception and I can find no way to get this function available in my module:

NameError: name 'displayHTML' is not defined

Sorry, I didn't put all the info in here; the links to the issues had all that so I didn't dump it all in here.

But again, I appreciate you replying. Have any ideas about using it from w/in an imported module?

Thanks!

Tom

T_1
New Contributor III

So is there really no way for Python code executing in a notebook (in a cell) to render HTML? It can only be done by an end user typing in displayHTML() themselves in a cell (passing in a string of HTML)? Other notebooks (Jupyter, Zeppelin) have functions for rendering HTML too, but code can call it (and it renders), or users can invoke it themselves too. Is there any way for executing Python code to render HTML in databricks?

Thanks,

Tom

User16764241763
Honored Contributor

Hello @Tom Weberโ€‹ 

I see the displayHTML function works in Python. Can you give it a try?

image

T_1
New Contributor III

I'm guessing that's just the same as typing it into a cell in a python notebook in the first place? Which obviously works. Executing it from inside a Python module is what doesn't work. Hope this picture comes through, and again, thanks for responding and trying to help. Hope this makes it more clear.

image

T_1
New Contributor III

So, still trying to get an answer on how code executing on behalf of what a user typed into a cell (they called a method from my package) can render HTML. Is there really no way for this to work in databricks? This works in Jupyter and Zeppelin, though they each have their own methods that have to be used (but they exist and I use them) Does databricks have no way for code to render HTML for a cell?

Thanks again,

Tom

ptweir
New Contributor III

I am facing exactly the same issue. Any progress?

T_1
New Contributor III

Hey @ptweir , I'm afraid I've not even gotten anyone to reply who understands the problem. Looking back, looks like I opened this a year and a half ago. I'm not sure if databricks developers monitor this; it seems to be a users helping users forum. We have one of those too, but I try to keep up with it since most of our users aren't really github types. So, I try to help on any channel they may use. I can't find anything that goes directly to databricks folks. So, no, I've gotten nothing on this. Sorry you're stuck on this too. ๐Ÿ˜ž

ptweir
New Contributor III

Looks like this works!

from IPython.display import HTML, display
from IPython import get_ipython
try:
   shell = get_ipython().__class__.__name__
   if shell == 'DatabricksShell':
       display(HTML(html_string))

kudos to this stackoverflow answer: https://stackoverflow.com/questions/71474139/how-to-import-displayhtml-in-databricks

 

T_1
New Contributor III

Interesting, that's just the Jupyter way to render HTML, which is my default. That didn't work back then, else I'd have never had an issue to start with! So, maybe they've realized the problem and fixed it silently? Well, I'm going to try to connect and test this out, but I'm still skeptical. But that's just my nature :). I'll post back after I can give this a try! If it's finally fixed, then that's great! I'll let you know ...

ptweir
New Contributor III

I'm using it in a python package here and just tested that it works in databricks.

T_1
New Contributor III

Holy Guacamole Batman! It works finally!!!! Wow, thanks @ptweir That's awesome! I can go back and update my doc (and code, to just use databricks the same, now, and Jupyter!) and it'll work by default. It's great they fixed it, shame they never told us. But I'll take the fix!

Thanks again for reaching out on this, that's great!

Thanks,

Tom

New Bitmap Image.png

โ€ƒ

ptweir
New Contributor III

Yay!!

Connect with Databricks Users in Your Area

Join a Regional User Group to connect with local Databricks users. Events will be happening in your city, and you wonโ€™t want to miss the chance to attend and share knowledge.

If there isnโ€™t a group near you, start one and help create a community that brings people together.

Request a New Group