21

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
  • 4
    Please don't put "solved" in the title. Instead add an answer to the question, then mark that answer as "accepted". That effectively marks the problem as solved. Commented Sep 13, 2019 at 16:26
  • 1
    Alternatively, simply delete your question if it's not going to help future visitors. Commented Sep 13, 2019 at 16:27
  • How did you solve it? Commented Dec 5, 2019 at 11:59
  • Yes, I just run these codes on jupyter notebook. Commented Dec 6, 2019 at 12:08

7 Answers 7

13

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)
Sign up to request clarification or add additional context in comments.

2 Comments

TypeError: write() argument must be str, not HTML
if you have HTML object obj, you should write obj.data, not obj
4

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.

Comments

4

Just use the data component of HTML Object.

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

Comments

2

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

Comments

2

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

plt.show()

Comments

1

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

Comments

0

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 : https://www.jetbrains.com/help/pycharm/reporting-issues.html

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.