How can I pretty print a JSON/dictionary value?

What I’m trying to do:

Pretty print JSON value for readability in order to display to user.

What I’ve tried and what’s not working:

Code Sample:

pretty_value = json.dumps(value_dict, indent = 2)

I get:

NotImplementedError: The ‘json’ module in Anvil is limited. You may only use a single argument to json.dumps() and json.loads().

Is there any easy alternative? Why is this limited in this way?

Thanks!

I think you should make this a feature request.

Client side python requires implementation so that it can run in the browser. If it’s not been implemented, it might be because no one has needed/requested it before…

related docs: Anvil Docs | Client-Side Python

Thanks, @stucork!
It also finally occurred to me in a half-sleep that I could just try it in javascript for this limited use case.

@hasib - the indent argument was easy enought to implement so that should go live soon.

If you want a temporary solution you can use json.dumps on the server until the client version goes live.

Or use the javascript version from window

from anvil.js.window import JSON
JSON.stringify(obj, None, 2)
2 Likes

Thanks, @stucork–I did go with the js.

I think it might now already be live if you try it.

2 Likes

I am still getting this error. Did you find any way to use json.load function?

If performance is not a problem you can pprint it in the server.

The interpreter in the server is a real Python interpreter and will behave as expected. Create the callable function in the server, call_s it from the client and show the result.