Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated json pane to accept single quote #2143

Merged
merged 3 commits into from
Apr 7, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion panel/models/json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export class JSONView extends PanelMarkupView {

render(): void {
super.render();
const text = this.model.text.replace(/(\r\n|\n|\r)/gm, "").replace("'", '"')
philippjfr marked this conversation as resolved.
Show resolved Hide resolved
const text = this.model.text
let json;
try {
json = window.JSON.parse(text)
Expand Down
10 changes: 6 additions & 4 deletions panel/pane/markup.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"""
import json
import textwrap
from ast import literal_eval

from six import string_types

Expand Down Expand Up @@ -338,10 +339,11 @@ def applies(cls, obj, **params):

def _get_properties(self):
properties = super()._get_properties()
if isinstance(self.object, string_types):
text = self.object
else:
text = json.dumps(self.object or {}, cls=self.encoder)
try:
data = literal_eval(self.object)
hoxbro marked this conversation as resolved.
Show resolved Hide resolved
except Exception:
data = self.object
text = json.dumps(data or {}, cls=self.encoder)
depth = None if self.depth < 0 else self.depth
return dict(text=text, theme=self.theme, depth=depth,
hover_preview=self.hover_preview, **properties)
18 changes: 17 additions & 1 deletion panel/tests/pane/test_markup.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,10 +201,26 @@ def test_json_pane(document, comm):
assert model.text == '{"a": 2}'
assert pane._models[model.ref['id']][0] is model

pane.object = '{"b": 3}'
pane.object = "{'b': 3}"
assert model.text == '{"b": 3}'
assert pane._models[model.ref['id']][0] is model

pane.object = {"test": "can't show this"}
assert model.text == '{"test": "can\'t show this"}'
assert pane._models[model.ref['id']][0] is model

pane.object = ["can't show this"]
assert model.text == '["can\'t show this"]'
assert pane._models[model.ref['id']][0] is model

pane.object = "can't show this"
assert model.text == '"can\'t show this"'
assert pane._models[model.ref['id']][0] is model

pane.object = "can show this"
assert model.text == '"can show this"'
assert pane._models[model.ref['id']][0] is model

# Cleanup
pane._cleanup(model)
assert pane._models == {}
Expand Down