Skip to content

Commit

Permalink
Updated json pane to accept single quote (#2143)
Browse files Browse the repository at this point in the history
  • Loading branch information
hoxbro committed Apr 7, 2021
1 parent 12d4994 commit 31b5350
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 5 deletions.
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("'", '"')
const text = this.model.text.replace(/(\r\n|\n|\r)/gm, "")
let json;
try {
json = window.JSON.parse(text)
Expand Down
9 changes: 5 additions & 4 deletions panel/pane/markup.py
Original file line number Diff line number Diff line change
Expand Up @@ -338,10 +338,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 = json.loads(self.object)
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)
16 changes: 16 additions & 0 deletions panel/tests/pane/test_markup.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,22 @@ def test_json_pane(document, comm):
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

0 comments on commit 31b5350

Please sign in to comment.