Skip to content

Commit 385ac03

Browse files
authored
Merge pull request #942 from Sage-Bionetworks/develop-schema-viz-tool-cors
Develop schema viz tool cors
2 parents a3ac31e + c27f1b4 commit 385ac03

File tree

16 files changed

+1399
-5
lines changed

16 files changed

+1399
-5
lines changed

api/openapi/api.yaml

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -849,3 +849,100 @@ paths:
849849
description: Check schematic log.
850850
tags:
851851
- Schema Operation
852+
/visualize/tangled_tree/layers:
853+
get:
854+
summary: Get layers of tangled tree.
855+
description: >-
856+
Get tangled tree node layers to display for a given data model and figure type
857+
operationId: api.routes.get_viz_tangled_tree_layers
858+
parameters:
859+
- in: query
860+
name: schema_url
861+
schema:
862+
type: string
863+
description: Data Model URL
864+
example: >-
865+
https://raw.githubusercontent.com/Sage-Bionetworks/schematic/develop/tests/data/example.model.jsonld
866+
required: true
867+
- in: query
868+
name: figure_type
869+
schema:
870+
type: string
871+
enum: ["component", "dependency"]
872+
description: Figure type to generate.
873+
example: 'component'
874+
required: true
875+
responses:
876+
"200":
877+
description: Returns a dataframe as a JSON String.
878+
content:
879+
text/json:
880+
schema:
881+
type: string
882+
tags:
883+
- Visualization Operations
884+
/visualize/tangled_tree/text:
885+
get:
886+
summary: Get text to display on tangled tree.
887+
description: >-
888+
Get tangled tree plain or higlighted text to display for a given data model, text formatting and figure type
889+
operationId: api.routes.get_viz_tangled_tree_text
890+
parameters:
891+
- in: query
892+
name: schema_url
893+
schema:
894+
type: string
895+
description: Data Model URL
896+
example: >-
897+
https://raw.githubusercontent.com/Sage-Bionetworks/schematic/develop/tests/data/example.model.jsonld
898+
required: true
899+
- in: query
900+
name: figure_type
901+
schema:
902+
type: string
903+
enum: ["component", "dependency"]
904+
description: Figure type to generate.
905+
example: 'component'
906+
required: true
907+
- in: query
908+
name: text_format
909+
schema:
910+
type: string
911+
enum: ["plain", "highlighted"]
912+
description: Text formatting type.
913+
example: 'plain'
914+
required: true
915+
responses:
916+
"200":
917+
description: Returns a dataframe as a JSON String.
918+
content:
919+
text/csv:
920+
schema:
921+
type: string
922+
tags:
923+
- Visualization Operations
924+
/visualize/attributes:
925+
get:
926+
summary: Get an attributes table for a data model, as a CSV (JSON String)
927+
description: >-
928+
Get all the attributes associated with a data model formatted as a
929+
dataframe (stored as a JSON String) for use in Observable visualization.
930+
operationId: api.routes.get_viz_attributes_explorer
931+
parameters:
932+
- in: query
933+
name: schema_url
934+
schema:
935+
type: string
936+
description: Data Model URL
937+
example: >-
938+
https://raw.githubusercontent.com/Sage-Bionetworks/schematic/develop/tests/data/example.model.jsonld
939+
required: true
940+
responses:
941+
"200":
942+
description: Returns a CSV as a JSON String.
943+
content:
944+
text/csv:
945+
schema:
946+
type: string
947+
tags:
948+
- Visualization Operations

api/routes.py

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,14 @@
1212

1313
from schematic import CONFIG
1414

15+
from schematic.visualization.attributes_explorer import AttributesExplorer
16+
from schematic.visualization.tangled_tree import TangledTree
1517
from schematic.manifest.generator import ManifestGenerator
1618
from schematic.models.metadata import MetadataModel
1719
from schematic.schemas.generator import SchemaGenerator
1820
from schematic.schemas.explorer import SchemaExplorer
1921
from schematic.store.synapse import SynapseStorage
22+
from flask_cors import CORS, cross_origin
2023
from schematic.schemas.explorer import SchemaExplorer
2124
import pandas as pd
2225
import json
@@ -349,7 +352,6 @@ def populate_manifest_route(schema_url, title=None, data_type=None):
349352

350353
return populated_manifest_link
351354

352-
353355
def get_storage_projects(input_token, asset_view):
354356
# call config handler
355357
config_handler(asset_view=asset_view)
@@ -374,7 +376,6 @@ def get_storage_projects_datasets(input_token, asset_view, project_id):
374376

375377
return sorted_dataset_lst
376378

377-
378379
def get_files_storage_dataset(input_token, asset_view, dataset_id, full_path, file_names=None):
379380
# call config handler
380381
config_handler(asset_view=asset_view)
@@ -389,13 +390,48 @@ def get_files_storage_dataset(input_token, asset_view, dataset_id, full_path, fi
389390
# call getFilesInStorageDataset function
390391
file_lst = store.getFilesInStorageDataset(datasetId=dataset_id, fileNames=file_names, fullpath=full_path)
391392
return file_lst
393+
392394
def get_component_requirements(schema_url, source_component, as_graph):
393395
metadata_model = initalize_metadata_model(schema_url)
394396

395397
req_components = metadata_model.get_component_requirements(source_component=source_component, as_graph = as_graph)
396398

397399
return req_components
398400

401+
def get_viz_attributes_explorer(schema_url):
402+
# call config_handler()
403+
config_handler()
404+
405+
temp_path_to_jsonld = get_temp_jsonld(schema_url)
406+
407+
attributes_csv = AttributesExplorer(temp_path_to_jsonld).parse_attributes(save_file=False)
408+
409+
return attributes_csv
410+
411+
def get_viz_tangled_tree_text(schema_url, figure_type, text_format):
412+
413+
temp_path_to_jsonld = get_temp_jsonld(schema_url)
414+
415+
# Initialize TangledTree
416+
tangled_tree = TangledTree(temp_path_to_jsonld, figure_type)
417+
418+
# Get text for tangled tree.
419+
text_df = tangled_tree.get_text_for_tangled_tree(text_format, save_file=False)
420+
421+
return text_df
422+
423+
def get_viz_tangled_tree_layers(schema_url, figure_type):
424+
425+
temp_path_to_jsonld = get_temp_jsonld(schema_url)
426+
427+
# Initialize Tangled Tree
428+
tangled_tree = TangledTree(temp_path_to_jsonld, figure_type)
429+
430+
# Get tangled trees layers JSON.
431+
layers = tangled_tree.get_tangled_tree_layers(save_file=False)
432+
433+
return layers[0]
434+
399435
def download_manifest(input_token, dataset_id, asset_view, as_json, new_manifest_name=''):
400436
# call config handler
401437
config_handler(asset_view=asset_view)

poetry.lock

Lines changed: 17 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ itsdangerous = "1.1.0"
6565
Jinja2 = "2.11.3"
6666
openpyxl = "^3.0.9"
6767
"backports.zoneinfo" = {markers = "python_version < \"3.9\"", version = "^0.2.1"}
68+
Flask-Cors = "^3.0.10"
6869

6970

7071
[tool.poetry.dev-dependencies]

run_api.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
# import our application
44
# Run our application
55
from api import create_app
6+
from flask_cors import CORS
67
import os
78

89
if __name__ == "__main__":
@@ -13,4 +14,5 @@
1314

1415
# Launch app
1516
app = create_app()
16-
app.run(host=host, port=port, debug=True)
17+
CORS(app, resources={r"*": {"origins": "*"}})
18+
app.run(port=3001, debug=True)

schematic/__main__.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111
from schematic.schemas.commands import (
1212
schema as schema_cli,
1313
) # schema conversion commands
14+
from schematic.visualization.commands import (
15+
viz as viz_cli,
16+
) # viz generation commands
1417
from schematic import init as init_cli # schematic initialization commands
1518

1619
logger = logging.getLogger()
@@ -34,6 +37,8 @@ def main():
3437
main.add_command(manifest_cli) # add manifest commands
3538
main.add_command(model_cli) # add model commands
3639
main.add_command(schema_cli) # add schema commands
40+
main.add_command(viz_cli) # add viz commands
41+
3742

3843

3944
if __name__ == "__main__":

schematic/help.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,3 +178,18 @@
178178
),
179179
}
180180
}
181+
182+
viz_commands = {
183+
"visualization": {
184+
"config": (
185+
"Specify the path to the `config.yml` using this option. This is a required argument."
186+
),
187+
"tangled_tree": {
188+
"figure_type": ("Specify the type of schema visualization to make. Either 'dependency' or 'component'."
189+
),
190+
"text_format": ("Specify the type of text to gather for tangled tree visualization, either 'plain' or 'highlighted'."
191+
),
192+
}
193+
}
194+
195+
}

schematic/schemas/explorer.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -617,6 +617,26 @@ def update_property(self, property_info):
617617
validate_schema(self.schema)
618618
logger.info(f"Updated the property {property_info['rdfs:label']} successfully.")
619619

620+
def get_nodes_descendants(self, graph, component):
621+
"""
622+
Return a list of nodes reachable from source in graph
623+
graph: networkx graph object
624+
component: any given node
625+
"""
626+
all_descendants = list(nx.descendants(graph, component))
627+
628+
return all_descendants
629+
630+
def get_nodes_ancestors(self, graph, component):
631+
"""
632+
Return a list of nodes reachable from source in graph
633+
graph: networkx graph object
634+
component: any given node
635+
"""
636+
all_ancestors = list(nx.ancestors(graph, component))
637+
638+
return all_ancestors
639+
620640
def get_digraph_by_edge_type(self, edge_type):
621641

622642
multi_digraph = self.schema_nx

schematic/schemas/generator.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import gc
12
import os
23
import json
34
import logging
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
from schematic.visualization.attributes_explorer import AttributesExplorer
2+
from schematic.visualization.tangled_tree import TangledTree

0 commit comments

Comments
 (0)