Giter Site home page Giter Site logo

Comments (6)

cjackal avatar cjackal commented on June 19, 2024 4

Another (less-effort) implementation:

import polars as pl
import io

def to_markdown(df: pl.DataFrame) -> str:
    buf = io.StringIO()
    with pl.Config(
        tbl_formatting="ASCII_MARKDOWN",
        tbl_hide_column_data_types=True,
        tbl_hide_dataframe_shape=True,
    ):
        print(df, file=buf)
    buf.seek(0)
    return buf.read()

One may need more config options like tbl_rows=-1 though.

from polars.

alexander-beedie avatar alexander-beedie commented on June 19, 2024 4

Another (less-effort) implementation:

Nice one; I think it's relatively unknown, but the Config object can also function as a decorator1, which works well with that pattern, eg:

@pl.Config( 
    tbl_formatting = "ASCII_MARKDOWN",        
    tbl_hide_column_data_types = True,
    tbl_hide_dataframe_shape = True,
)
def frame_to_markdown( df: pl.DataFrame ) -> str:
    return str(df)

If we could add left/right/center align options to the markdown output we'd have the basics covered at least 🤔

Footnotes

  1. https://github.com/pola-rs/polars/pull/9307

from polars.

MarcoGorelli avatar MarcoGorelli commented on June 19, 2024 1

pandas does this by deferring to tabulate - there's a feature there about adding support for polars astanin/python-tabulate#258
I guess contributing Polars support to tabulate would be the easiest way to make it happen

from polars.

cjackal avatar cjackal commented on June 19, 2024 1

@MarcoGorelli I thought the opposite trying to implement pyarrow table support to it a while ago; the way tabulate support pandas is a bit too rough (checking hasattr(df, "value"), hasattr(df, "index"), hasattr(df.index, "name"), ..., ) for other third party dataframe libraries to support it without first converting to pandas.

from polars.

baggiponte avatar baggiponte commented on June 19, 2024

I also received a feature request to cast to HTML; if one is accepted then the other would be pretty much straighforward.

from polars.

deanm0000 avatar deanm0000 commented on June 19, 2024

doesn't _repr_html already cast it to html or am I misunderstanding something?

from polars.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.