mwouts / itables

Pandas DataFrames as Interactive DataTables
https://mwouts.github.io/itables/
MIT License
710 stars 53 forks source link

Export buttons with formatted data frame #259

Closed brentgunderson closed 2 months ago

brentgunderson commented 2 months ago

I just started using ITables, what a great package!

Interestingly, when I try to format a data frame, I seem to lose the ability to add export buttons.

Example below (ITables 2.0.0)

import string

import numpy as np
import pandas as pd
from itables import init_notebook_mode, show
from itables.sample_dfs import get_countries

init_notebook_mode()

df = get_countries(html=False)

# Add columns for the searchPanes demo
df["climate_zone"] = np.where(
    df["latitude"].abs() < 23.43615,
    "Tropical",
    np.where(
        df["latitude"].abs() < 35,
        "Sub-tropical",
        # Artic circle is 66.563861 but there is no capital there => using 64
        np.where(df["latitude"].abs() < 64, "Temperate", "Frigid"),
    ),
)
df["hemisphere"] = np.where(df["latitude"] > 0, "North", "South")

wide_df = pd.DataFrame(
    {
        letter: np.random.normal(size=100)
        for letter in string.ascii_lowercase + string.ascii_uppercase
    }
)

show(df, buttons=["copyHtml5", "csvHtml5", "excelHtml5"])

show(
    df.style.format({"longitude": "{:,.1f}"}),
    buttons=["pageLength", "copyHtml5", "csvHtml5", "excelHtml5"],
)

image

joshy commented 2 months ago

I am not sure why pagination is shown instead of buttons but if you configure the location, it will be shown:

show(
    df.style.format({"longitude": "{:,.1f}"}),
    buttons=["pageLength", "copyHtml5", "csvHtml5", "excelHtml5"],
    layout =  {"topStart":"buttons"}
)
mwouts commented 2 months ago

Thank you @brentgunderson for the detailed report, and @joshy for the workaround! I will have a look soon - I would expect itables to set the "buttons" on the layout too but apparently this is not the case here.