Skip to content

Commit 472c62e

Browse files
authored
Escape HTML characters when generating results table (#341)
* Escape HTML characters when generating results table * update changelog * add fix for SPARQL Co-authored-by: Michael Chin <chnmch@amazon.com>
1 parent 738875d commit 472c62e

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

ChangeLog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ Starting with v1.31.6, this file will contain a record of major features and upd
1010
- Added silent output option to additional magics ([Link to PR](https://github.com/aws/graph-notebook/pull/326))
1111
- Fixed %sparql_status magic to return query status without query ID ([Link to PR](https://github.com/aws/graph-notebook/pull/337))
1212
- Fixed incorrect Gremlin query --store-to output ([Link to PR](https://github.com/aws/graph-notebook/pull/334))
13+
- Fixed certain characters not displaying correctly in results table ([Link to PR](https://github.com/aws/graph-notebook/pull/341))
1314
- Reverted Gremlin console tab to single results column ([Link to PR](https://github.com/aws/graph-notebook/pull/330))
1415
- Bumped jquery-ui from 1.13.1 to 1.13.2 (([Link to PR](https://github.com/aws/graph-notebook/pull/328))
1516

src/graph_notebook/magics/graph_magic.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,16 @@ def generate_pagination_vars(visible_results: int):
178178
return visible_results_fixed, pagination_options, pagination_menu
179179

180180

181+
def replace_html_chars(result):
182+
html_char_map = {"&": "&amp;", "<": "&lt;", ">": "&gt;", '"': "&quot;", "'": "&#39;"}
183+
fixed_result = str(result)
184+
185+
for k, v in iter(html_char_map.items()):
186+
fixed_result = fixed_result.replace(k, v)
187+
188+
return fixed_result
189+
190+
181191
# TODO: refactor large magic commands into their own modules like what we do with %neptune_ml
182192
# noinspection PyTypeChecker
183193
@magics_class
@@ -468,6 +478,8 @@ def sparql(self, line='', cell='', local_ns: dict = None):
468478
rows_and_columns = sparql_get_rows_and_columns(results)
469479
if rows_and_columns is not None:
470480
results_df = pd.DataFrame(rows_and_columns['rows'])
481+
results_df = results_df.astype(str)
482+
results_df = results_df.applymap(lambda x: replace_html_chars(x))
471483
results_df.insert(0, "#", range(1, len(results_df) + 1))
472484
for col_index, col_name in enumerate(rows_and_columns['columns']):
473485
try:
@@ -743,7 +755,10 @@ def gremlin(self, line, cell, local_ns: dict = None):
743755
# If not, then render our own HTML template.
744756
results_df = pd.DataFrame(query_res)
745757
if not results_df.empty:
746-
query_res_reformat = [[result] for result in query_res]
758+
query_res_reformat = []
759+
for result in query_res:
760+
fixed_result = replace_html_chars(result)
761+
query_res_reformat.append([fixed_result])
747762
query_res_reformat.append([{'__DUMMY_KEY__': ['DUMMY_VALUE']}])
748763
results_df = pd.DataFrame(query_res_reformat)
749764
results_df.drop(results_df.index[-1], inplace=True)
@@ -1929,6 +1944,8 @@ def handle_opencypher_query(self, line, cell, local_ns):
19291944
if rows_and_columns:
19301945
titles.append('Console')
19311946
results_df = pd.DataFrame(rows_and_columns['rows'])
1947+
results_df = results_df.astype(str)
1948+
results_df = results_df.applymap(lambda x: replace_html_chars(x))
19321949
results_df.insert(0, "#", range(1, len(results_df) + 1))
19331950
for col_index, col_name in enumerate(rows_and_columns['columns']):
19341951
results_df.rename({results_df.columns[col_index + 1]: col_name},
@@ -1969,6 +1986,8 @@ def handle_opencypher_query(self, line, cell, local_ns):
19691986
if rows_and_columns:
19701987
titles.append('Console')
19711988
results_df = pd.DataFrame(rows_and_columns['rows'])
1989+
results_df = results_df.astype(str)
1990+
results_df = results_df.applymap(lambda x: replace_html_chars(x))
19721991
results_df.insert(0, "#", range(1, len(results_df) + 1))
19731992
for col_index, col_name in enumerate(rows_and_columns['columns']):
19741993
results_df.rename({results_df.columns[col_index + 1]: col_name},

0 commit comments

Comments
 (0)