How to display ?

I try to run the below codes but I have a problem in showing the results. also, I use pycharm IDE.

from fastai.text import *
data = pd.read_csv("data_elonmusk.csv", encoding='latin1')
data.head()
data = (TextList.from_df(data, cols='Tweet') .split_by_rand_pct(0.1) .label_for_lm() .databunch(bs=48))
data.show_batch()

The output while I run the line "data.show_batch()" is:

IPython.core.display.HTML object
4

7 Answers

If you don't want to work within a Jupyter Notebook you can save data as an HTML file and open it in a browser.

with open("data.html", "w") as file: file.write(data)
2

You can only render HTML in a browser and not in a Python console/editor environment.

Hence it works in Jupiter notebook, Jupyter Lab, etc.

At best you call .data to see HTML, but again it will not render.

I solved my problem by running the codes on Jupiter Notebook.

You could add this code after data.show_batch():

plt.show()

Just use the data component of HTML Object.

with open("data.html", "w") as file: file.write(data.data)

Another option besides writing it do a file is to use an HTML parser in Python to programatically edit the HTML. The most commonly used tool in Python is beautifulsoup. You can install it via

pip install beautifulsoup4

Then in your program you could do

from bs4 import BeautifulSoup
html_string = data.show_batch().data
soup = BeautifulSoup(html_string)
# do some manipulation to the parsed HTML object
# then do whatever else you want with the object

So, I'm guessing your true question here is

Why does The pycharm IDE does not render html correctly

After a few tests, it seems that the PyCharm rendering does not support some html tags. The following work correctly:

display(HTML('<section><h1>Hello</h1><p>Text</p></section>'))

Valid case where html is rendered

But the following will output <IPython.core.display.HTML object>

display(HTML('<table><tr><td>Hello</td></tr></table>'))

Invalid case, html is not rendered

So the reason you are getting this result is because the HTML rendered contains some tags unsupported by PyCharm. Your workaround of using a real notebook is the correct thing to do, but I encourage you to open a bug to Jetbrains :

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

You Might Also Like