plotly / plotly/dash

dcc.send_data_frame polars support

Open
#3,153 3 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

feature P2
Dominant language
Python
Stars
24.4k
Forks
2.3k
Avg merge
2d 7h
Merged PRs (30d)
13

Description

Currently dcc.send_data_frame only supports pandas-style writers. I was wondering if polars support can be added as well. Previously, I used to do all my operations in polars and then on the final step when I need to download the dataframe I converted it to pandas to utilize the dcc.send_data_frame function. However, now im using my solution below. I am wondering if we can add a feature to support both pandas and polars via the send_data_frame function

Workaround solution to use polars to send dataframe:

import polars as pl
from dash import Dash, html, dcc, callback, Output, Input
import io
import base64

def polars_to_send_data_frame(df: pl.DataFrame, filename: str, **csv_kwargs):
    buffer = io.StringIO()
    df.write_csv(buffer, **csv_kwargs)
    
    return {
        'content': base64.b64encode(buffer.getvalue().encode('utf-8')).decode('utf-8'),
        'filename': filename,
        'type': 'text/csv',
        'base64': True
    }

app = Dash(__name__)

# Sample data
df = pl.DataFrame({
    'A': range(5),
    'B': ['foo', 'bar', 'baz', 'qux', 'quux'],
    'C': [1.1, 2.2, 3.3, 4.4, 5.5]
})

app.layout = html.Div([
    html.Button("Download CSV", id="btn"),
    dcc.Download(id="download")
])

@callback(
    Output("download", "data"),
    Input("btn", "n_clicks"),
    prevent_initial_call=True
)
def download_csv(n_clicks):
    return polars_to_send_data_frame(df, "data.csv")

if __name__ == '__main__':
    app.run(debug=True)

I would love to contribute and make a pr to add polars support!

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by locating the dcc.send_data_frame entry point and its existing tests for pandas-style writers. Add coverage for the requested Polars dataframe support alongside the current behavior, and consider the provided workaround as the expected CSV behavior. Done means dcc.send_data_frame accepts Polars dataframes without requiring a pandas conversion.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
data
Issue type
Feature
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.