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 all commits
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.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