Skip to content

Commit

Permalink
Add --results-per-page query option (aws#242)
Browse files Browse the repository at this point in the history
* Add -r option

* Replace interval value

* Add upper limit

Co-authored-by: Michael Chin <[email protected]>
  • Loading branch information
michaelnchin and michaelnchin authored Feb 2, 2022
1 parent 2202df7 commit 1dc71f8
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 6 deletions.
27 changes: 24 additions & 3 deletions src/graph_notebook/magics/graph_magic.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,15 @@ def query_type_to_action(query_type):
return 'sparqlupdate'


def results_per_page_check(results_per_page):
if results_per_page < 1:
return 1
elif results_per_page > 1000:
return 1000
else:
return int(results_per_page)


# TODO: refactor large magic commands into their own modules like what we do with %neptune_ml
# noinspection PyTypeChecker
@magics_class
Expand Down Expand Up @@ -279,6 +288,8 @@ def sparql(self, line='', cell='', local_ns: dict = None):
parser.add_argument('-sd', '--simulation-duration', type=int, default=1500,
help='Specifies maximum duration of visualization physics simulation. Default is 1500ms')
parser.add_argument('--silent', action='store_true', default=False, help="Display no query output.")
parser.add_argument('-r', '--results-per-page', type=int, default=10,
help='Specifies how many query results to display per page in the output. Default is 10')
parser.add_argument('--no-scroll', action='store_true', default=False,
help="Display the entire output without a scroll bar.")
args = parser.parse_args(line.split())
Expand Down Expand Up @@ -365,8 +376,10 @@ def sparql(self, line='', cell='', local_ns: dict = None):
rows_and_columns = sparql_get_rows_and_columns(results)
if rows_and_columns is not None:
table_id = f"table-{str(uuid.uuid4())[:8]}"
visible_results = results_per_page_check(args.results_per_page)
first_tab_html = sparql_table_template.render(columns=rows_and_columns['columns'],
rows=rows_and_columns['rows'], guid=table_id)
rows=rows_and_columns['rows'], guid=table_id,
amount=visible_results)

# Handling CONSTRUCT and DESCRIBE on their own because we want to maintain the previous result
# pattern of showing a tsv with each line being a result binding in addition to new ones.
Expand Down Expand Up @@ -486,6 +499,8 @@ def gremlin(self, line, cell, local_ns: dict = None):
parser.add_argument('-sd', '--simulation-duration', type=int, default=1500,
help='Specifies maximum duration of visualization physics simulation. Default is 1500ms')
parser.add_argument('--silent', action='store_true', default=False, help="Display no query output.")
parser.add_argument('-r', '--results-per-page', type=int, default=10,
help='Specifies how many query results to display per page in the output. Default is 10')
parser.add_argument('--no-scroll', action='store_true', default=False,
help="Display the entire output without a scroll bar.")

Expand Down Expand Up @@ -581,7 +596,9 @@ def gremlin(self, line, cell, local_ns: dict = None):
f'unable to create gremlin network from result. Skipping from result set: {value_error}')

table_id = f"table-{str(uuid.uuid4()).replace('-', '')[:8]}"
first_tab_html = gremlin_table_template.render(guid=table_id, results=query_res)
visible_results = results_per_page_check(args.results_per_page)
first_tab_html = gremlin_table_template.render(guid=table_id, results=query_res,
amount=visible_results)

if not args.silent:
metadata_output = widgets.Output(layout=gremlin_layout)
Expand Down Expand Up @@ -1661,6 +1678,8 @@ def handle_opencypher_query(self, line, cell, local_ns):
parser.add_argument('-sd', '--simulation-duration', type=int, default=1500,
help='Specifies maximum duration of visualization physics simulation. Default is 1500ms')
parser.add_argument('--silent', action='store_true', default=False, help="Display no query output.")
parser.add_argument('-r', '--results-per-page', type=int, default=10,
help='Specifies how many query results to display per page in the output. Default is 10')
parser.add_argument('--no-scroll', action='store_true', default=False,
help="Display the entire output without a scroll bar.")
args = parser.parse_args(line.split())
Expand Down Expand Up @@ -1723,8 +1742,10 @@ def handle_opencypher_query(self, line, cell, local_ns):
titles.append('Console')
if rows_and_columns is not None:
table_id = f"table-{str(uuid.uuid4())[:8]}"
visible_results = results_per_page_check(args.results_per_page)
table_html = opencypher_table_template.render(columns=rows_and_columns['columns'],
rows=rows_and_columns['rows'], guid=table_id)
rows=rows_and_columns['rows'], guid=table_id,
amount=visible_results)

# Display Graph Tab (if exists)
if force_graph_output:
Expand Down
12 changes: 11 additions & 1 deletion src/graph_notebook/visualization/templates/gremlin_table.html
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,23 @@
</table>
<script type="text/javascript">
require(["datatables"], function (datatables) {
function sort_remove_dup_ary(ary) {
return ary.sort(function(a, b){return a-b}).filter(function(item, pos, ary) { return !pos || item != ary[pos - 1] })
}

var paginationAry = sort_remove_dup_ary([{{amount}}, 10, 25, 50, 100, -1]);
var optionsAry = sort_remove_dup_ary([{{amount}}, 10, 25, 50, 100, "All"]);

var dt = $('#{{guid}}').DataTable({
scrollY: true,
scrollX: true,
//scrollCollapse: true,
columnDefs: [
{targets: [0], width: "5%"},
{targets: [1], minWidth: "95%"}
]
],
"lengthMenu": [paginationAry, optionsAry],
"pageLength": {{amount}}
});

var wrapper = $('#{{guid}}_wrapper');
Expand Down
11 changes: 10 additions & 1 deletion src/graph_notebook/visualization/templates/opencypher_table.html
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,22 @@
</table>
<script type="text/javascript">
require(["datatables"], function (datatables) {
function sort_remove_dup_ary(ary) {
return ary.sort(function(a, b){return a-b}).filter(function(item, pos, ary) { return !pos || item != ary[pos - 1] })
}

var paginationAry = sort_remove_dup_ary([{{amount}}, 10, 25, 50, 100, -1]);
var optionsAry = sort_remove_dup_ary([{{amount}}, 10, 25, 50, 100, "All"]);

$('#{{guid}}').DataTable({
scrollY: true,
scrollX: true,
columnDefs: [
{targets: [0], width: "5%"},
],
bAutoWidth: true
bAutoWidth: true,
"lengthMenu": [paginationAry, optionsAry],
"pageLength": {{amount}}
});
})
</script>
Expand Down
11 changes: 10 additions & 1 deletion src/graph_notebook/visualization/templates/sparql_table.html
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,22 @@
</table>
<script type="text/javascript">
require(["datatables"], function (datatables) {
function sort_remove_dup_ary(ary) {
return ary.sort(function(a, b){return a-b}).filter(function(item, pos, ary) { return !pos || item != ary[pos - 1] })
}

var paginationAry = sort_remove_dup_ary([{{amount}}, 10, 25, 50, 100, -1]);
var optionsAry = sort_remove_dup_ary([{{amount}}, 10, 25, 50, 100, "All"]);

$('#{{guid}}').DataTable({
scrollY: true,
scrollX: true,
columnDefs: [
{targets: [0], width: "5%"},
],
bAutoWidth: true
bAutoWidth: true,
"lengthMenu": [paginationAry, optionsAry],
"pageLength": {{amount}}
});
})
</script>
Expand Down

0 comments on commit 1dc71f8

Please sign in to comment.