I have a Python Flask app that recently switched from using Pandas to Polars for some dataframe handling. The pertinent code is shown here:
data = { 'Text': ['Virginia Woolf, Mrs. Dalloway', 'College website corpus', 'Presidential inaugural speeches', 'Federalist Papers', 'British Novels', 'YOUR TEXT'],
'To Be Frequency': [28.3, 16.7, 31.8, 39.8, 31.4, results[1]] }
df = pd.from_dict(data)
# textresult = (df.sort_values(by=['To Be Frequency'], ascending=False)).style
# See https://pola-rs.github.io/polars/py-polars/html/reference/config.html for complete list of Polars.Config settings
pd.Config.set_tbl_hide_column_data_types(True)
pd.Config.set_tbl_hide_dataframe_shape(True)
pd.Config.set_fmt_str_lengths(40)
pd.Config.set_tbl_width_chars(200)
textresult = df.sort( 'To Be Frequency' )._repr_html_( ) # convert the result to HTML because a simple string won't do
The textresult
output looks like this:
I'm looking for any way to remove the double quotes that surround my textresult
output. Suggestions?
I've tried every conceivable pd.Config
value as well as different approaches to declaration of the data
dictionary. I've also searched for any CSS that might easily let me "hide" the quotes. Nothing has worked thus far.
I'm expecting the table you see in the posted image, but with no quotes around the "Text" values.
---Update---
Based on comments received I found that a simple print(df)
returns the frame in my console without quotes around Text values, but I need HTML output suitable for rendering in a Flask template so it seems the root of my problem is in the .repr_html( ) representation only?
---Update---
Moments ago I added an enhancement request to the Polars issue queue. In conjunction with that issue I wrote a test and tried to modify pertinent functions in py-polars/polars/_html.py
but could not craft an elegant solution; I just don't know enough about @HTMLFormatter and related features yet. So, my simple fix was to modify my code to look like this:
htmlresult = df.sort( 'To Be Frequency' )._repr_html_( ) # convert the result to HTML because a simple string won't do
textresult = htmlresult.replace(""", "")
It's a kludge but a quick solution to my very simple use of Polars.