Pycharm displays object class instead of the object itself

Asked

Viewed 142 times

0

Hello,

I am training the use of some packages in Python 3.x, and use Pycharm Community 2020.3.3. When I try to customize graphs and tables using packages like pandas and Seaborn Pycharm does not display what I want, but its class. I’ve tried with the print traditional and with the display imported from Ipython. Here is a short example:

import pandas as pd
from IPython.display import display, HTML
import seaborn as sns

paulistinha = {"Rodada 1": [3, 3, 1, 0], "Rodada 2": [3, 1, 0, 3], "Rodada 3": [3, 3, 3, 3], "Rodada 4": [3, 0, 3, 1]}
linhas = ["Palmeiras", "Corinthians", "São Paulo", "Santos"]
classifica = pd.DataFrame(paulistinha, index=linhas)

display(HTML(classifica.to_html()))
dfa = classifica.head(2)
display(HTML(dfa.to_html()))

cm = sns.light_palette('green', as_cmap=True)
s = classifica.style.background_gradient(cmap=cm)
print(s)
display(s)

print(classifica.boxplot())

When I try to run these 5 examples instead of receiving color tables or a nice boxplot chart, I get the following output:

<IPython.core.display.HTML object>
<IPython.core.display.HTML object>
<pandas.io.formats.style.Styler object at 0x017104F0>
<pandas.io.formats.style.Styler object at 0x017104F0>
AxesSubplot(0.125,0.11;0.775x0.77)

In the case of controls .to_html and .boxplot(), Pycharm’s inspection tells me Expected type 'tuple', got 'xxxx' instead, where xxxx = HTML in the case of .to_html e xxxx = Styler for the .boxplot().

When I play the same codes on Google Colab smooth wheel.

I searched here in Stack Overflow in PT and found absolutely nothing similar. In English, I even find some things similar, but since I don’t know very well the language I don’t know how close are the solutions of what I need.

1 answer

1


It is displayed directly on Google Colab because the expression is evaluated the way it was sent, the purpose is different for the environment. To make the Plots are displayed in your local code you need to call the method show() of matplotlib.pyplot, something like:

Start of file, importing module:

import matplotlib.pyplot as plt

And to display the plot, we define the same with the method boxplot and we call the method show:

classifica.boxplot()
plt.show()

The result would be:

Gráfico exibindo resultado da expressão

For cases where you call the methods to display the content in HTML, you are calling the method display (that displays the evaluated expression passed as argument in outputs available) and calling methods and functions that transform the expressions into HTML, as in one of the lines:

display(HTML(classifica.to_html()))

Notice that in the line above, two functions are called to transform the current object into a string in HTML, but the function HTML() is not returning a string but a new type of object, therefore receives it described in the output (terminal, in this case):

<IPython.core.display.HTML object>

For more information about type objects and functions available on Ipython, please refer your documentation deeper.

Already just the expression to_html() of your object classifica returns a string. You will get the HTML return correctly when using the function display to display it. As in the example:

display(classifica.to_html())

The result would be:

<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th></th>
      <th>Rodada 1</th>
      <th>Rodada 2</th>
      <th>Rodada 3</th>
      <th>Rodada 4</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>Palmeiras</th>
      <td>3</td>
      <td>3</td>
      <td>3</td>
      <td>3</td>
    </tr>
    <tr>
      <th>Corinthians</th>
      <td>3</td>
      <td>1</td>
      <td>3</td>
      <td>0</td>
    </tr>
    <tr>
      <th>São Paulo</th>
      <td>1</td>
      <td>0</td>
      <td>3</td>
      <td>3</td>
    </tr>
    <tr>
      <th>Santos</th>
      <td>0</td>
      <td>3</td>
      <td>3</td>
      <td>1</td>
    </tr>
  </tbody>
</table>

For stylized HTML, you can get a string in HTML from the method render in the return of the stylization of their Plot:

s = classifica.style.background_gradient(cmap=cm)
rendered_plot = s.render()
display(rendered_plot)

As a result:

<style  type="text/css" >
#T_2b2a5_row0_col0,#T_2b2a5_row0_col1,#T_2b2a5_row0_col3,#T_2b2a5_row1_col0,#T_2b2a5_row2_col3,#T_2b2a5_row3_col1{
            background-color:  #008000;
            color:  #f1f1f1;
        }#T_2b2a5_row0_col2,#T_2b2a5_row1_col2,#T_2b2a5_row1_col3,#T_2b2a5_row2_col1,#T_2b2a5_row2_col2,#T_2b2a5_row3_col0,#T_2b2a5_row3_col2{
            background-color:  #ebf3eb;
            color:  #000000;
        }#T_2b2a5_row1_col1,#T_2b2a5_row2_col0,#T_2b2a5_row3_col3{
            background-color:  #9ccd9c;
            color:  #000000;
        }</style><table id="T_2b2a5_" ><thead>    <tr>        <th class="blank level0" ></th>        <th class="col_heading level0 col0" >Rodada 1</th>        <th class="col_heading level0 col1" >Rodada 2</th>        <th class="col_heading level0 col2" >Rodada 3</th>        <th class="col_heading level0 col3" >Rodada 4</th>    </tr></thead><tbody>
                <tr>
                        <th id="T_2b2a5_level0_row0" class="row_heading level0 row0" >Palmeiras</th>
                        <td id="T_2b2a5_row0_col0" class="data row0 col0" >3</td>
                        <td id="T_2b2a5_row0_col1" class="data row0 col1" >3</td>
                        <td id="T_2b2a5_row0_col2" class="data row0 col2" >3</td>
                        <td id="T_2b2a5_row0_col3" class="data row0 col3" >3</td>
            </tr>
            <tr>
                        <th id="T_2b2a5_level0_row1" class="row_heading level0 row1" >Corinthians</th>
                        <td id="T_2b2a5_row1_col0" class="data row1 col0" >3</td>
                        <td id="T_2b2a5_row1_col1" class="data row1 col1" >1</td>
                        <td id="T_2b2a5_row1_col2" class="data row1 col2" >3</td>
                        <td id="T_2b2a5_row1_col3" class="data row1 col3" >0</td>
            </tr>
            <tr>
                        <th id="T_2b2a5_level0_row2" class="row_heading level0 row2" >São Paulo</th>
                        <td id="T_2b2a5_row2_col0" class="data row2 col0" >1</td>
                        <td id="T_2b2a5_row2_col1" class="data row2 col1" >0</td>
                        <td id="T_2b2a5_row2_col2" class="data row2 col2" >3</td>
                        <td id="T_2b2a5_row2_col3" class="data row2 col3" >3</td>
            </tr>
            <tr>
                        <th id="T_2b2a5_level0_row3" class="row_heading level0 row3" >Santos</th>
                        <td id="T_2b2a5_row3_col0" class="data row3 col0" >0</td>
                        <td id="T_2b2a5_row3_col1" class="data row3 col1" >3</td>
                        <td id="T_2b2a5_row3_col2" class="data row3 col2" >3</td>
                        <td id="T_2b2a5_row3_col3" class="data row3 col3" >1</td>
            </tr>
    </tbody></table>

As expressions are evaluated differently in Google Colab, so that you don’t have to copy and paste the HTML code into some site or file and evaluate it in the browser, I believe the library imgkit with wkhtmltopdf be a good choice, saving the content evaluated in a local file, as in the example:

import imgkit
imgkit.from_string(classifica.to_html(), 'rendered_plot.png') 

More information on how to do the imgkit run correctly are in your documentation, I believe the included references have more information if you need.

  • It worked perfect for the boxplot, but not for the Ipython tables (sorts.to_html) and not for the Pandas tables (classifies.style.background_gradient). I suppose it’s the same kind of problem (some command), but I can’t find it. Actually, I found a place talking to convert to float, but it didn’t work.

  • @Giovannivm edited the answer and supplemented it with some information which I believe is what you need.

  • Cool, that settles one more question. Only the last is missing from the color table with Abor and pandas ( cm = sns.light_palette('green', as_cmap=True) / s = sort.style.background_gradient(Cmap=cm) ). Solutions to turn to float did not work. I’m sorry I have so many questions in one, when I created the question I thought the three of you would have a unique solution.

  • @Giovannivm what you mentioned about color tables is in the part where I explain how to use your Plot stylization: "For stylized HTML, you can get an HTML string from the render method in the return of your Plot styling..." The following snippet displays the table with colors as described.

  • Man, I worked like a dog, I spent a long time understanding every part of what you wrote, I fucked a lot of the imgkit package and wkhtmltopdf, and finally I was able to understand and solve. Sensational. Thank you so much.

  • If you allow me one last comment-question, to not create another question: the difference between Pycharm and Google Colab, in this case, is that Colab "talks" with another language (HTML, in this case) while the free version of Pycharm does not?

  • No. Google Colab (which uses the Jupyter Notebook) is not an "IDE" or code editor, it is an environment that evaluates the code when sent, remembering the REPL from Python. When using the REPL you will notice that both work in a similar way, evaluating the code at the time of sending. If you run your Python code from the outside and/or write it without Pycharm you will notice the same behavior, because Pycharm is just an editor in this regard.

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.