Direct the output of df.info() to web page

How to direct the output of df.info() to web page. Currently the ouput of st.write(df.info()) and st.text(df.info()) both are being redirected to prompt console.

Hi @cyruslentin, welcome to the Streamlit community!! :wave: :partying_face:

The pandas documentation for df.info says, by default, the output is printed to sys.stdout. This behavior is governed by the buf parameter which defaults to sys.stdout.

To display the output in your Streamlit app, pipe the output of df.info to a buffer instead of sys.stdout, get the buffer content, and display it with st.text like so:

import streamlit as st
import pandas as pd
import io

df = pd.DataFrame(
    {
        "foo": ["hello", "world", "foo " * 30],
        "bar": ["hello", "world", "bar" * 30],
        "baz": [1, 2, 3],
    }
)

buffer = io.StringIO()
df.info(buf=buffer)
s = buffer.getvalue()

st.text(s)

Output:

Happy Streamlit’ing! :balloon:
Snehan

7 Likes

@snehankekre - thank you for the lucid explanation along with the solution. The solution you provided worked. Thanks again

2 Likes