Problem: Streamlir shows source code, when dataframe is displayed

Dear all,

My code reads a file and then displays it in a data frame.

uploaded_file = st.file_uploader("Choose a KML file")

if uploaded_file is not None:
    
    df = gpd.read_file (uploaded_file, driver ='KML')

    st.write(df)

but for some reason it also displays part of the source code of the data frame.

Does anyone know how to solve this?

Thank you! :slight_smile:

Hi @karina-castillo, welcome to the forum! :wave: :partying_face:

Have you tried using st.dataframe(df) or st.table(df) instead?

Happy Streamlit’ing! :balloon:
Snehan

Hi @snehankekre ! thanks for your welcome :slight_smile:

I have un other problem with st.dataframe() or st.table

:frowning:

I use streamlit 1.0

@karina-castillo One workaround is to convert the values of the geometry column to type str, like so:

if uploaded_file is not None:
    
    df = gpd.read_file (uploaded_file, driver ='KML')
    df['geometry'] = df.geometry.astype(str)

    st.dataframe(df)

I will defer to @randyzwitch on native Streamlit support, or lack thereof, for geopandas :slightly_smiling_face:

Best, :balloon:
Snehan

ok thank you for your help!

Just for information, with your solution @snehankekre df['geometry'] = df.geometry.astype(str) I have another error:

So i will wait if @randyzwitch has any other suggestions.

Thanks again!

I’m too familiar with the internals of geopandas, but it looks like your KML file might have a sentinel value for missing in it? So that does not get converted to geom type, and then when you try to use the method it chokes.

Can you try something like df['geometry_str'] = [str(x) for x in df['geometry']? Pretty sure that base Python falls back to calling repr_str or something similar so almost anything works.

Best,
Randy

Hello @randyzwitch , thank you for your reply. The KML files are not the problem, they are not missing any value. In this case I wanted to use Streamlit to display the results of a project I have done and so far the only problem I have had is with displaying the data from the KML files in a data frame, for the rest of the calculations I have no problem. It is true that the “geometry” column of the KML files is quite special, I think that simply disturbs it because it is something very specific to Geopandas. I tried what you advised me but it didn’t work either, the best result I got was using st.write, but that piece of code comes out as I show in my first post.

I think the solution to this problem is more complex to deal with and is directly related to geopandas, for now I will simply not show a data frame, the rest of the code works fine.

Thank you for your help!

Best,

Karina

1 Like

If you do figure it out, please let us know!