quarto-dev / quarto-cli

Open-source scientific and technical publishing system built on Pandoc.
https://quarto.org
Other
3.82k stars 309 forks source link

Table styling not available in `docx` #10139

Open heartoforchid opened 3 months ago

heartoforchid commented 3 months ago

Bug description

I use Pandas Styler to display a table in a colored fashion. Quarto returns a non-colored table while the code works correctly in other settings

Steps to reproduce

---
title: "Reproducible Quarto Document"
format: html
engine: jupyter
---

```{python}
import pandas as pd

# Define the color scheme
deep_red = '#FF0000'
light_red = '#FFC7CE'
white = '#FFFFFF'
light_green = '#C6EFCE'
deep_green = '#00B050'

# Create a simple DataFrame to demonstrate the issue
data = {
    'Index': ['Equities (%)', 'Fixed Income (bps)'],
    '1W': [5, -1],
    'YTD': [10, -3]
}
df = pd.DataFrame(data)

# Function to highlight cells based on values
def highlight_cells(val):
    if val < 0:
        return f'background-color: {light_red}; color: black'
    elif val == 0:
        return f'background-color: {white}; color: black'
    elif val > 0:
        return f'background-color: {light_green}; color: black'

# Apply styling
styled_df = df.style.applymap(highlight_cells, subset=['1W', 'YTD'])

# Display styled DataFrame
styled_df

[Here is the reference doc] custom-reference-doc.docx


### Expected behavior

<img width="237" alt="Screenshot 2024-06-26 at 10 27 57" src="https://github.com/quarto-dev/quarto-cli/assets/73822223/04615fb9-128a-4463-8ecf-1b694c06e4fa">

### Actual behavior

<img width="353" alt="Screenshot 2024-06-26 at 10 41 16" src="https://github.com/quarto-dev/quarto-cli/assets/73822223/66ae475d-c25b-4c15-a1fd-62d7ff8ee187">

### Your environment

_No response_

### Quarto check output

Quarto 1.5.46 [✓] Checking versions of quarto binary dependencies... Pandoc version 3.2.0: OK Dart Sass version 1.70.0: OK Deno version 1.41.0: OK Typst version 0.11.0: OK [✓] Checking versions of quarto dependencies......OK [✓] Checking Quarto installation......OK Version: 1.5.46 Path: /Applications/quarto/bin

[✓] Checking tools....................OK TinyTeX: (external install) Chromium: (not installed)

[✓] Checking LaTeX....................OK Using: TinyTex Path: /Users/lacey/Library/TinyTeX/bin/universal-darwin Version: 2023

[✓] Checking basic markdown render....OK

[✓] Checking Python 3 installation....OK Version: 3.8.19 (Conda) Path: /opt/anaconda3/bin/python Jupyter: 5.7.2 Kernels: python3

[✓] Checking Jupyter engine render....OK

[✓] Checking R installation...........OK Version: 4.3.2 Path: /Library/Frameworks/R.framework/Resources LibPaths:

[✓] Checking Knitr engine render......OK

mcanouil commented 3 months ago

Could you properly format your post using code blocks for code and terminal outputs? Thanks. If your code contains code blocks, you need to enclose it using more backticks, i.e., usually four ````.

See https://quarto.org/bug-reports.html#formatting-make-githubs-markdown-work-for-us.

heartoforchid commented 3 months ago
---
title: "Reproducible Quarto Document"
format: html
engine: jupyter
---

```{python}
import pandas as pd

# Define the color scheme
deep_red = '#FF0000'
light_red = '#FFC7CE'
white = '#FFFFFF'
light_green = '#C6EFCE'
deep_green = '#00B050'

# Create a simple DataFrame to demonstrate the issue
data = {
    'Index': ['Equities (%)', 'Fixed Income (bps)'],
    '1W': [5, -1],
    'YTD': [10, -3]
}
df = pd.DataFrame(data)

# Function to highlight cells based on values
def highlight_cells(val):
    if val < 0:
        return f'background-color: {light_red}; color: black'
    elif val == 0:
        return f'background-color: {white}; color: black'
    elif val > 0:
        return f'background-color: {light_green}; color: black'

# Apply styling
styled_df = df.style.applymap(highlight_cells, subset=['1W', 'YTD'])

# Display styled DataFrame
styled_df

[Here is the reference doc] custom-reference-doc.docx

mcanouil commented 3 months ago

@heartoforchid Simply edit your original post to make it legible.