Skip to content

Commit

Permalink
Minor widget updates (jupyrdf#97)
Browse files Browse the repository at this point in the history
Replace default QueryWidget in library. Minor improvements to related code.
  • Loading branch information
zwelz3 authored Mar 2, 2021
1 parent 51adb39 commit 8f2c4f5
Show file tree
Hide file tree
Showing 12 changed files with 479 additions and 142 deletions.
169 changes: 169 additions & 0 deletions examples/QueryWidget.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Query Widget\n",
"\n",
"A simple widget for construting and visualizing a SPARQL query and its results.\n",
"\n",
"Reusable self-contained widgets are preferrable to monolithic widgets. Therefore, we\n",
"break down the widget components available in `ipyradiant` that are aggregated into a\n",
"unified `QueryWidget`."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"First, we load an example graph file from the library data."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from ipyradiant import FileManager, PathLoader\n",
"\n",
"lw = FileManager(loader=PathLoader(path=\"data\"))\n",
"lw.loader.file_picker.value = lw.loader.file_picker.options[\"starwars.ttl\"]\n",
"lw"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## QueryPreview\n",
"\n",
"The `QueryPreview` widget allows users to enter a `query` in the left panel, and see\n",
"live syntax highlighting in the right panel.\n",
"\n",
"In a future update, it may be possible to provide syntax highlighting and tips as part\n",
"of a single query entry widget."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from ipyradiant.query.visualize import QueryPreview\n",
"\n",
"qp = QueryPreview()\n",
"# Specify an example query for demonstration pu\n",
"qp.query = \"\"\"\\\n",
"PREFIX voc: <https://swapi.co/vocabulary/>\n",
"CONSTRUCT {\n",
" ?s ?p ?o .\n",
" voc:Character a rdfs:Class .\n",
"} WHERE {\n",
" {\n",
" SELECT DISTINCT ?s\n",
" WHERE {\n",
" ?s a voc:Character .\n",
" }\n",
" LIMIT 3\n",
" }\n",
" ?s ?p ?o .\n",
"}\n",
"\"\"\"\n",
"qp"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## QueryResultsGrid\n",
"\n",
"The `QueryResultsGrid` provides a simple way to view the results of a query as a grid.\n",
"\n",
"A future update may include the ability to apply operations on the grid (i.e.\n",
"filtering)."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from ipyradiant.query.visualize import QueryResultsGrid\n",
"\n",
"qrg = QueryResultsGrid(namespaces=dict(lw.graph.namespaces()))\n",
"# set the query results for the demonstration\n",
"qrg.query_result = lw.graph.query(qp.query)\n",
"qrg"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## QueryWidget\n",
"\n",
"The `QueryWidget` aggregates the `QueryPreview` and `QueryResultsGrid` together with a\n",
"\"Run Query\" button to support the workflow of query specification, execution, and\n",
"analysis of results.\n",
"\n",
"> Tip: The results of the query can be collected via `QueryWidget.query_result`"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from ipyradiant.query.app import QueryWidget\n",
"\n",
"qw = QueryWidget(graph=lw.graph)\n",
"# specify an example query for demonstration\n",
"qw.query = \"\"\"\\\n",
"CONSTRUCT {\n",
" ?s ?p ?o .\n",
" voc:Character a rdfs:Class .\n",
"} WHERE {\n",
" {\n",
" SELECT DISTINCT ?s\n",
" WHERE {\n",
" ?s a voc:Character .\n",
" }\n",
" LIMIT 3\n",
" }\n",
" ?s ?p ?o .\n",
"}\n",
"\"\"\"\n",
"# automate the execution of the query for demonstration\n",
"qw.run_button.click()\n",
"qw"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.10"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
23 changes: 5 additions & 18 deletions examples/RemoteQuery_Example.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -218,30 +218,17 @@
"outputs": [],
"source": [
"widget = WidgetExample()\n",
"widget.query.query_constructor.query_type = \"SELECT DISTINCT\"\n",
"widget.query.query_constructor.query_line = \"*\"\n",
"widget.query.query_constructor.query_body = \"\"\"\n",
"{\n",
"widget.query.query = \"\"\"\\\n",
"SELECT DISTINCT *\n",
"WHERE {\n",
" SERVICE <http://dbpedia.org/sparql>\n",
" {\n",
" SELECT ?s ?p ?o\n",
" WHERE {?s ?p ?o}\n",
" LIMIT 10\n",
" }\n",
"}\n",
"\"\"\"\n",
"widget.query.query_constructor.formatted_query.value = \"\"\"\n",
"SELECT DISTINCT *\n",
"WHERE { \n",
" SERVICE <http://dbpedia.org/sparql>\n",
" {\n",
" SELECT ?s ?p ?o\n",
" WHERE {?s ?p ?o}\n",
" LIMIT 10\n",
" }\n",
" }\n",
"\"\"\"\n",
"widget.query"
"\"\"\""
]
},
{
Expand Down Expand Up @@ -389,7 +376,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.9"
"version": "3.7.10"
}
},
"nbformat": 4,
Expand Down
18 changes: 7 additions & 11 deletions examples/Tab_App_Example.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@
" graph = T.Instance(Graph, allow_none=True)\n",
" file_manager = T.Instance(FileManager)\n",
" query = T.Instance(QueryWidget)\n",
" vis = T.Instance(CytoscapeVisualizer)\n",
" log = W.Output()\n",
"\n",
" def __init__(self, graph: Graph = None, *args, **kwargs):\n",
Expand All @@ -64,12 +63,10 @@
" super().__init__(*args, **kwargs)\n",
" T.link((self.file_manager, \"graph\"), (self, \"graph\"))\n",
" T.link((self, \"graph\"), (self.query, \"graph\"))\n",
" T.link((self, \"graph\"), (self.vis, \"graph\"))\n",
"\n",
" self.children = [self.file_manager, self.query, self.vis]\n",
" self.children = [self.file_manager, self.query]\n",
" self.set_title(0, \"Load\")\n",
" self.set_title(1, \"Query\")\n",
" self.set_title(2, \"Visualize\")\n",
"\n",
" @T.default(\"graph\")\n",
" def make_default_graph(self):\n",
Expand All @@ -81,11 +78,10 @@
"\n",
" @T.default(\"query\")\n",
" def make_default_query_widget(self):\n",
" return QueryWidget()\n",
"\n",
" @T.default(\"vis\")\n",
" def make_vis_widget(self):\n",
" return CytoscapeVisualizer()"
" qw = QueryWidget()\n",
" # set default query\n",
" qw.query = \"\"\"SELECT DISTINCT ?s ?p ?o\\nWHERE {\\n ?s ?p ?o .\\n}\\nLIMIT 10\"\"\"\n",
" return qw"
]
},
{
Expand All @@ -102,7 +98,7 @@
"outputs": [],
"source": [
"tabs = RadiantTabs()\n",
"W.VBox([tabs, tabs.query.log])"
"tabs"
]
}
],
Expand All @@ -122,7 +118,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.8"
"version": "3.7.10"
}
},
"nbformat": 4,
Expand Down
25 changes: 18 additions & 7 deletions examples/Test_Tab_App.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
"outputs": [],
"source": [
"pl = tabs.file_manager.loader\n",
"vis = tabs.vis\n",
"q = tabs.query"
]
},
Expand Down Expand Up @@ -98,6 +97,21 @@
" print(f\"[{key}]\", f\"+{int(delta)}\", msg)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"TEST_QUERY = \"\"\"\\\n",
"SELECT DISTINCT ?s ?p ?o\n",
"WHERE {\n",
" ?s ?p ?o .\n",
"}\n",
"LIMIT 5\n",
"\"\"\""
]
},
{
"cell_type": "code",
"execution_count": null,
Expand All @@ -111,19 +125,16 @@
" timestamp(p, \"starting...\")\n",
" tabs.graph = Graph()\n",
" timestamp(p, \"cleaned...\")\n",
" assert not vis.cyto_widget.graph.edges\n",
" assert not vis.cyto_widget.graph.nodes\n",
" tabs.selected_index = 0\n",
" timestamp(p, f\"loading...\")\n",
" pl.file_picker.value = pl.file_picker.options[p]\n",
" timestamp(p, f\"... {len(pl.graph)} triples loaded\")\n",
" assert len(pl.graph)\n",
" tabs.selected_index = 1\n",
" timestamp(p, \"querying...\")\n",
" q.query = TEST_QUERY\n",
" q.run_button.click()\n",
" tabs.selected_index = 2\n",
" assert vis.cyto_widget.graph.edges\n",
" assert vis.cyto_widget.graph.nodes\n",
" assert len(q.query_result) > 0, \"Failed to execute query.\"\n",
" timestamp(p, \"OK!\")\n",
" except Exception as err:\n",
" timestamp(p, \"ERROR\")\n",
Expand Down Expand Up @@ -185,7 +196,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.8"
"version": "3.7.10"
}
},
"nbformat": 4,
Expand Down
Loading

0 comments on commit 8f2c4f5

Please sign in to comment.