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.

pdflatex builder failed to display styled dataframes

See original GitHub issue

Describe the problem

When creating a jupyter book with a styled dataframe as described in the Jupyter Book docs here (https://jupyterbook.org/content/code-outputs.html#library-output-formatting), the dataframe is not displayed with the pdf latex builder and instead we are presented with

<pandas.io.formats.style.Styler at 0x1de96f473d0>

image

Link to your repository or website

No response

Steps to reproduce

  1. Create a new Jupyter book with a ipynb containing the following code referenced from (https://jupyterbook.org/content/code-outputs.html#library-output-formatting)
import numpy as np
import pandas as pd

np.random.seed(24)
df = pd.DataFrame({'A': np.linspace(1, 10, 10)})
df = pd.concat([df, pd.DataFrame(np.random.randn(10, 4), columns=list('BCDE'))],
               axis=1)
df.iloc[3, 3] = np.nan
df.iloc[0, 2] = np.nan

def color_negative_red(val):
    """
    Takes a scalar and returns a string with
    the css property `'color: red'` for negative
    strings, black otherwise.
    """
    color = 'red' if val < 0 else 'black'
    return 'color: %s' % color

def highlight_max(s):
    '''
    highlight the maximum in a Series yellow.
    '''
    is_max = s == s.max()
    return ['background-color: yellow' if v else '' for v in is_max]

df.style.\
    applymap(color_negative_red).\
    apply(highlight_max).\
    set_table_attributes('style="font-size: 10px"')
  1. Build the book as a latexpdf using
jupyter-book build testreport --builder=pdflatex
  1. Open the PDF and see that where the dataframe should have been, there is instead
<pandas.io.formats.style.Styler at 0x1de96f473d0>

The version of Python you’re using

python 3.8.5

Your operating system

Windows 10

Versions of your packages

Jupyter Book      : 0.12.1
External ToC      : 0.2.3
MyST-Parser       : 0.15.2
MyST-NB           : 0.13.1
Sphinx Book Theme : 0.1.7
Jupyter-Cache     : 0.4.3
NbClient          : 0.5.3

Additional context

No response

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
andrewellis55commented, Dec 21, 2021

Quick update on this, I’ve found both the seeming cause and a workaround. Once we call df.style, we no longer have a pd.Dataframe but rather a pd.io.formats.style.Styler. The Styler object does not have _repr_latex_ defined that is required to render it in latex but it does have a to_latex() method. Which means we can just add the following lines to our code to get it to work

    def rep_latex():
        return df.to_latex(convert_css=True)
    df._repr_latex_ = rep_latex

Now once we have this, we will need to make sure a few more packages are added to our latex build. This can be done by adding the following lines to our _conf.yml

sphinx:
  config:
    latex_elements:
      # Note this line cheats the table parameter into the documentclass[] parameter
      papersize: letterpaper,table
      # These lines add the desired packages to the latex build
      preamble: |+
        \usepackage{xcolor}
        \usepackage{booktabs}
        \usepackage{multirow}
1reaction
franzhaascommented, Jan 6, 2022

I think this is the same issue.

https://github.com/executablebooks/jupyter-book/issues/1512

According to pandas, this should be improvded by the 1.4 release, which is currently on the way.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Bibtex finished with 3 error messages - LaTeX Stack Exchange
I'm in AUCTeX under Win 7. This is the route to follow, isn't it? compile main.tex with pdflatex; launch BibTex on main.
Read more >
pyspark show dataframe as table with horizontal scroll in ...
Open the jupyter notebook ../site-packages/notebook/static/style/style.min.css file. Search for white-space: pre-wrap; , and remove it. Save ...
Read more >
pandas.DataFrame — pandas 1.5.2 documentation
Return an int representing the number of elements in this object. style. Returns a Styler object. ... Compare to another DataFrame and show...
Read more >
Formatting code outputs - Jupyter Book
See the Pandas Styling docs for more information about styling DataFrames, ... By default, the standard output/error streams and text/plain MIME outputs may ......
Read more >
Creating PDF Reports with Pandas, Jinja and WeasyPrint
For example, if you want to put two DataFrames on one Excel sheet, you need to ... a fully composed HTML table with...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

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