question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

`st.write`/`st.json` render sets as strings

See original GitHub issue

Summary

When I try to save a set in st.session_state and write it to the screen using st.write, the set is printed out as a string.

Steps to reproduce

Code snippet:

import streamlit as st

if ["list"] not in st.session_state:
    st.session_state["list"] = []
if ["dict"] not in st.session_state:
    st.session_state["dict"] = {}
if ["set"] not in st.session_state:
    st.session_state["set"] = set()

st.write(st.session_state)

Expected behavior:

{
  "list": [],
  "set": set(),
  "dict": {}
}

Actual behavior:

{
  "list": [],
  "set": "set()",
  "dict": {}
}

Is this a regression?

No.

Debug info

  • Streamlit version: 1.10
  • Python version: 3.8
  • Using virtualenv
  • OS version: Ubuntu 20.04 on WSL2
  • Browser version: Firefox 102

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
sfc-gh-kbregulacommented, Aug 17, 2022

Alternatively, we can create our own serialization instead of JSON, which allows two additional types to be transferred: set and tuple.

When it comes to displaying in UI, we can adopt a pretty-format format, that is, add a new label that describes the type.

Before:

{
  "list": [],
  "set": "set()",
  "dict": {}
}

After:

Object {
  "list": Array [],
  "set": Set {},
  "dict": Object {}
}

This, however, will require us to make a fork of the library - react-json-view, which will support our data serialization format.

1reaction
vdonatocommented, Aug 16, 2022

@sfc-gh-tszerszen +1 to this behavior. I think it’d probably be more desirable than nicely rendering it as a python set since, even though the types aren’t quite right, it’d be more useful for a user to be able to expand/collapse the list with existing functionality in st.json.

I think this is a small decision that the product team would have to make the final call on, so CCing @jrieke to take a look at when he’s back from vacation 🌴

Read more comments on GitHub >

github_iconTop Results From Across the Web

API Reference - Streamlit Docs
Streamlit makes it easy for you to visualize, mutate, and share data. The API reference is organized by activity type, like displaying data...
Read more >
Rendering a JsonResult to a string in Razor - Stack Overflow
Create a string property and save the JSON as a string, then use JSON.parse in your View to convert it to an object....
Read more >
Loading JSON data from Cloud Storage | BigQuery
Shows how to load JSON files from Cloud Storage into a new table, or append to, or overwrite a table. Shows how to...
Read more >
19.3 Using the Object Model API - Oracle Help Center
keySet method returns a set of strings that contains the keys in the object, and the JsonObject.get(String name) method returns the value of...
Read more >
Component Reference - Apache JMeter - User's Manual
If set, the JMeter sampler will check if the response is a redirect and follow it if so. ... GraphQL query (or mutation)...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found