Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dcc.send_data_frame polars support #3153

Open
omarirfa opened this issue Feb 6, 2025 · 1 comment
Open

dcc.send_data_frame polars support #3153

omarirfa opened this issue Feb 6, 2025 · 1 comment
Labels
feature something new P2 considered for next cycle

Comments

@omarirfa
Copy link

omarirfa commented Feb 6, 2025

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!

@gvwilson gvwilson added feature something new P2 considered for next cycle labels Feb 13, 2025
@gvwilson
Copy link
Contributor

thanks @omarirfa - this is a good suggestion and I'll see if we can get it into our next cycle.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature something new P2 considered for next cycle
Projects
None yet
Development

No branches or pull requests

2 participants