ankane/ruby-polars

Request: add support for `.sink_ndjson(<file-like object>, ...)`

DeflateAwning opened this issue · 3 comments

I'm hoping to be able to pass a file-like object (e.g., StringIO, or any other file-like object) to .sink_ndjson(file_like_object). Currently, this method only supports the first arg ("path") being a string.

I believe this request is very similar to #51.

The end goal is to be able to sink the output of .sink_ndjson(...) directly to Rails to return it as a streaming response.

Here's an example of how I'm hoping to use it in the end:

# endpoint:

  def stream_ndjson
    response.headers['Content-Type'] = 'application/x-ndjson'
    response.headers['Content-Disposition'] = 'attachment; filename="data.ndjson"'

    # Assuming `df` is your data frame-like object ready to stream NDJSON
    # And `sink_ndjson` can write directly to an IO object
    df.sink_ndjson(response.stream)
  ensure
    response.stream.close
  end

Extension

  • Should probably be applied to all sink function (e.g., .sink_csv(...), etc.)

Darn, you're totally right

Requested in main polars directly: pola-rs/polars#16085