def json( self, body, *, # keyword-only arguments: expanded=True, ): """Display object or string as a pretty-printed JSON string. Parameters ---------- body : Object or str The object to print as JSON. All referenced objects should be serializable to JSON as well. If object is a string, we assume it contains serialized JSON. expanded : bool An optional boolean that allows the user to set whether the initial state of this json element should be expanded. Defaults to True. This argument can only be supplied by keyword. Example ------- >>> st.json({ ... 'foo': 'bar', ... 'baz': 'boz', ... 'stuff': [ ... 'stuff 1', ... 'stuff 2', ... 'stuff 3', ... 'stuff 5', ... ], ... }) .. output:: https://share.streamlit.io/streamlit/docs/main/python/api-examples-source/data.json.py height: 385px """ import streamlit as st if isinstance(body, AutoSessionState): body = body.to_dict() if not isinstance(body, str): try: body = json.dumps(body, default=repr) except TypeError as err: st.warning( "Warning: this data structure was not fully serializable as " "JSON due to one or more unexpected keys. (Error was: %s)" % err) body = json.dumps(body, skipkeys=True, default=repr) json_proto = JsonProto() json_proto.body = body json_proto.expanded = expanded return self.dg._enqueue("json", json_proto)
def json(self, body): """Display object or string as a pretty-printed JSON string. Parameters ---------- body : Object or str The object to print as JSON. All referenced objects should be serializable to JSON as well. If object is a string, we assume it contains serialized JSON. Example ------- >>> st.json({ ... 'foo': 'bar', ... 'baz': 'boz', ... 'stuff': [ ... 'stuff 1', ... 'stuff 2', ... 'stuff 3', ... 'stuff 5', ... ], ... }) .. output:: https://static.streamlit.io/0.25.0-2JkNY/index.html?id=CTFkMQd89hw3yZbZ4AUymS height: 280px """ import streamlit as st if isinstance(body, LazySessionState): body = body.to_dict() if not isinstance(body, str): try: body = json.dumps(body, default=repr) except TypeError as err: st.warning( "Warning: this data structure was not fully serializable as " "JSON due to one or more unexpected keys. (Error was: %s)" % err ) body = json.dumps(body, skipkeys=True, default=repr) json_proto = JsonProto() json_proto.body = body return self.dg._enqueue("json", json_proto)