-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1195 from briehl/spec-editor
TASK-1201 - Simple viewer for App Spec files
- Loading branch information
Showing
8 changed files
with
225 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
from IPython.display import Javascript | ||
from jinja2 import Template | ||
from biokbase.narrative import clients | ||
import json | ||
|
||
|
||
def generate_app_cell(validated_spec=None, spec_tuple=None): | ||
"""Produces an invisible blob of JavaScript that inserts a new cell in the notebook, | ||
and crams the validated_spec in it. It then removes itself, so it won't happen again | ||
on page reload. | ||
For the inputs, validated_spec > spec_tuple. That is, if validated_spec is present, | ||
that's always used. if spec_tuple is there, and validated_spec is not, then the | ||
tuple's used. | ||
Also, the tuple should be (spec_json, display_yaml), all as strings. | ||
""" | ||
|
||
if spec_tuple is not None and validated_spec is None: | ||
nms = clients.get("narrative_method_store") | ||
validated = nms.validate_method({ | ||
"id": "some_test_app", | ||
"spec_json": spec_tuple[0], | ||
"display_yaml": spec_tuple[1] | ||
}) | ||
if validated.get('is_valid', 0) == 1: | ||
validated_spec = validated['method_spec'] | ||
elif "errors" in validated and validated['errors']: | ||
raise Exception(validated['errors']) | ||
|
||
js_template = """ | ||
var outputArea = this, | ||
cellElement = outputArea.element.parents('.cell'), | ||
cellIdx = Jupyter.notebook.get_cell_elements().index(cellElement), | ||
thisCell = Jupyter.notebook.get_cell(cellIdx), | ||
spec_json = '{{spec}}', | ||
cellData = { | ||
type: 'devapp', | ||
appTag: 'dev', | ||
appSpec: JSON.parse(spec_json) | ||
}; | ||
Jupyter.narrative.insertAndSelectCell('code', 'below', cellIdx, cellData); | ||
""" | ||
js_code = Template(js_template).render(spec=json.dumps(validated_spec)) | ||
|
||
return Javascript(data=js_code, lib=None, css=None) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
# | ||
# define display information | ||
# | ||
name: Test Simple Inputs | ||
tooltip: | | ||
A simple test spec with a single input. | ||
screenshots: [] | ||
|
||
icon: icon.png | ||
|
||
# | ||
# define a set of similar methods that might be useful to the user | ||
# | ||
suggestions: | ||
apps: | ||
related: | ||
[] | ||
next: | ||
[] | ||
methods: | ||
related: | ||
[] | ||
next: | ||
[] | ||
|
||
# | ||
# Configure the display and description of parameters | ||
# | ||
parameters : | ||
simple_string : | ||
ui-name : | | ||
A String | ||
short-hint : | | ||
A description string. | ||
description : | | ||
<p>This is a tiny, simple method intended for testing the Narrative's ability to parse and display specs.</p> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
{ | ||
"ver": "0.0.1", | ||
"authors": [ | ||
"wjriehl" | ||
], | ||
"contact": "help@kbase.us", | ||
"visible": true, | ||
"categories": ["active"], | ||
"app_type": "editor", | ||
"widgets": { | ||
"input": null, | ||
"output": null | ||
}, | ||
"parameters": [ | ||
{ | ||
"id": "simple_string", | ||
"optional": false, | ||
"advanced": false, | ||
"allow_multiple": false, | ||
"default_values": [ "" ], | ||
"field_type": "text", | ||
"text_options": { | ||
} | ||
} | ||
], | ||
"behavior": { | ||
"service-mapping": { | ||
"url": "", | ||
"name": "NarrativeTest", | ||
"method": "generic_test", | ||
"input_mapping": [ | ||
{ | ||
"input_parameter": "simple_string", | ||
"target_property": "simple_string" | ||
} | ||
], | ||
"output_mapping": [ | ||
{ | ||
"service_method_output_path": [0,"simple_string"], | ||
"target_property": "simple_string" | ||
} | ||
] | ||
} | ||
}, | ||
"job_id_output_field": "docker" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
""" | ||
Some tests for the App Editor module. | ||
""" | ||
import unittest | ||
from biokbase.narrative.appeditor import ( | ||
generate_app_cell | ||
) | ||
import json | ||
from util import TestConfig | ||
|
||
|
||
class AppEditorTestCase(unittest.TestCase): | ||
@classmethod | ||
def setUpClass(self): | ||
config = TestConfig() | ||
self.specs_list = config.load_json_file(config.get('specs', 'app_specs_file')) | ||
self.spec_json = config.load_json_file(config.get('specs', 'simple_spec_json')) | ||
with open(config.file_path(config.get('specs', 'simple_display_yaml'))) as f: | ||
self.display_yaml = f.read() | ||
f.close() | ||
|
||
def test_gen_app_cell_post_validation(self): | ||
js = generate_app_cell(validated_spec=self.specs_list[0]) | ||
self.assertIsNotNone(js) | ||
|
||
def test_gen_app_cell_pre_valid(self): | ||
js = generate_app_cell(spec_tuple=(json.dumps(self.spec_json), self.display_yaml)) | ||
self.assertIsNotNone(js) | ||
|
||
def test_gen_app_cell_fail_validation(self): | ||
with self.assertRaises(Exception): | ||
generate_app_cell(spec_tuple=("{}", self.display_yaml)) |