def do_sparql_query_save_result(self, query, res):
     request_generator = create_request_generator(
         AuthModeEnum.IAM, IAMAuthCredentialsProvider.ENV)
     try:
         res['result'] = do_sparql_query(query, self.host, self.port,
                                         self.ssl, request_generator)
     except requests.HTTPError as exception:
         res['error'] = exception.response.json()
    def test_do_sparql_query(self):
        query = "SELECT * WHERE {?s ?p ?o} LIMIT 1"
        request_generator = SPARQLRequestGenerator()

        res = do_sparql_query(query, self.host, self.port, self.ssl,
                              request_generator)
        self.assertEqual(type(res), dict)
        self.assertTrue('s' in res['head']['vars'])
        self.assertTrue('p' in res['head']['vars'])
        self.assertTrue('o' in res['head']['vars'])
示例#3
0
 def test_do_sparql_query(self):
     query = "SELECT * WHERE {?s ?p ?o} LIMIT 1"
     request_generator = create_request_generator(
         AuthModeEnum.IAM, IAMAuthCredentialsProvider.ENV)
     res = do_sparql_query(query, self.host, self.port, self.ssl,
                           request_generator)
     self.assertEqual(type(res), dict)
     self.assertTrue('s' in res['head']['vars'])
     self.assertTrue('p' in res['head']['vars'])
     self.assertTrue('o' in res['head']['vars'])
 def do_sparql_query_save_result(self, query, res):
     try:
         res['result'] = do_sparql_query(query, self.host, self.port, self.ssl, SPARQLRequestGenerator())
     except requests.HTTPError as exception:
         res['error'] = exception.response.json()
示例#5
0
        def on_button_clicked(b=None):
            submit_button.close()
            language_dropdown.disabled = True
            data_set_drop_down.disabled = True
            language = language_dropdown.value.lower()
            data_set = data_set_drop_down.value.lower()
            with output:
                print(f'Loading data set {data_set} with language {language}')
            queries = get_queries(language, data_set)
            if len(queries) < 1:
                with output:
                    print('Did not find any queries for the given dataset')
                return

            load_index = 1  # start at 1 to have a non-empty progress bar
            progress = widgets.IntProgress(
                value=load_index,
                min=0,
                max=len(queries) + 1,  # len + 1 so we can start at index 1
                orientation='horizontal',
                bar_style='info',
                description='Loading:')

            with progress_output:
                display(progress)

            for q in queries:
                with output:
                    print(f'{progress.value}/{len(queries)}:\t{q["name"]}')
                # Just like with the load command, seed is long-running
                # as such, we want to obtain the values of host, port, etc. in case they
                # change during execution.
                host = self.graph_notebook_config.host
                port = self.graph_notebook_config.port
                auth_mode = self.graph_notebook_config.auth_mode
                ssl = self.graph_notebook_config.ssl

                if language == 'gremlin':
                    client_provider = create_client_provider(
                        auth_mode, self.graph_notebook_config.
                        iam_credentials_provider_type)
                    # IMPORTANT: We treat each line as its own query!
                    for line in q['content'].splitlines():
                        try:
                            do_gremlin_query(line, host, port, ssl,
                                             client_provider)
                        except GremlinServerError as gremlinEx:
                            try:
                                error = json.loads(
                                    gremlinEx.args[0]
                                    [5:])  # remove the leading error code.
                                content = json.dumps(error, indent=2)
                            except Exception:
                                content = {'error': gremlinEx}

                            with output:
                                print(content)
                            progress.close()
                            return
                        except Exception as e:
                            content = {'error': e}
                            with output:
                                print(content)
                            progress.close()
                            return
                else:
                    request_generator = create_request_generator(
                        auth_mode, self.graph_notebook_config.
                        iam_credentials_provider_type)
                    try:
                        do_sparql_query(q['content'], host, port, ssl,
                                        request_generator)
                    except HTTPError as httpEx:
                        # attempt to turn response into json
                        try:
                            error = json.loads(
                                httpEx.response.content.decode('utf-8'))
                            content = json.dumps(error, indent=2)
                        except Exception:
                            content = {'error': httpEx}
                        with output:
                            print(content)
                        progress.close()
                        return
                    except Exception as ex:
                        content = {'error': str(ex)}
                        with output:
                            print(content)

                        progress.close()
                        return

                progress.value += 1

            progress.close()
            with output:
                print('Done.')
            return
示例#6
0
    def sparql(self, line='', cell=''):
        request_generator = create_request_generator(
            self.graph_notebook_config.auth_mode,
            self.graph_notebook_config.iam_credentials_provider_type,
            command='sparql')

        if line != '':
            mode = str_to_query_mode(line)
        else:
            mode = self.mode
        tab = widgets.Tab()
        logger.debug(f'using mode={mode}')
        if mode == QueryMode.EXPLAIN:
            html_raw = sparql_explain(cell, self.graph_notebook_config.host,
                                      self.graph_notebook_config.port,
                                      self.graph_notebook_config.ssl,
                                      request_generator)
            explain_output = widgets.Output(layout=DEFAULT_LAYOUT)
            with explain_output:
                display(HTML(html_raw))

            tab.children = [explain_output]
            tab.set_title(0, 'Explain')
            display(tab)
        else:
            query_type = get_query_type(cell)
            headers = {} if query_type not in [
                'SELECT', 'CONSTRUCT', 'DESCRIBE'
            ] else {
                'Accept': 'application/sparql-results+json'
            }
            res = do_sparql_query(cell, self.graph_notebook_config.host,
                                  self.graph_notebook_config.port,
                                  self.graph_notebook_config.ssl,
                                  request_generator, headers)
            titles = []
            children = []

            display(tab)
            table_output = widgets.Output(layout=DEFAULT_LAYOUT)
            # Assign an empty value so we can always display to table output.
            # We will only add it as a tab if the type of query allows it.
            # Because of this, the table_output will only be displayed on the DOM if the query was of type SELECT.
            table_html = ""
            query_type = get_query_type(cell)
            if query_type in ['SELECT', 'CONSTRUCT', 'DESCRIBE']:
                logger.debug('creating sparql network...')

                # some issues with displaying a datatable when not wrapped in an hbox and displayed last
                hbox = widgets.HBox([table_output], layout=DEFAULT_LAYOUT)
                titles.append('Table')
                children.append(hbox)

                expand_all = line == '--expand-all'
                sn = SPARQLNetwork(expand_all=expand_all)
                sn.extract_prefix_declarations_from_query(cell)
                try:
                    sn.add_results(res)
                except ValueError as value_error:
                    logger.debug(value_error)

                logger.debug(f'number of nodes is {len(sn.graph.nodes)}')
                if len(sn.graph.nodes) > 0:
                    f = Force(network=sn,
                              options=self.graph_notebook_vis_options)
                    titles.append('Graph')
                    children.append(f)
                    logger.debug('added sparql network to tabs')

                rows_and_columns = get_rows_and_columns(res)
                if rows_and_columns is not None:
                    table_id = f"table-{str(uuid.uuid4())[:8]}"
                    table_html = sparql_table_template.render(
                        columns=rows_and_columns['columns'],
                        rows=rows_and_columns['rows'],
                        guid=table_id)

                # 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.
                if query_type == 'CONSTRUCT' or query_type == 'DESCRIBE':
                    lines = []
                    for b in res['results']['bindings']:
                        lines.append(
                            f'{b["subject"]["value"]}\t{b["predicate"]["value"]}\t{b["object"]["value"]}'
                        )
                    raw_output = widgets.Output(layout=DEFAULT_LAYOUT)
                    with raw_output:
                        html = sparql_construct_template.render(lines=lines)
                        display(HTML(html))
                    children.append(raw_output)
                    titles.append('Raw')

            json_output = widgets.Output(layout=DEFAULT_LAYOUT)
            with json_output:
                print(json.dumps(res, indent=2))
            children.append(json_output)
            titles.append('JSON')

            tab.children = children
            for i in range(len(titles)):
                tab.set_title(i, titles[i])

            with table_output:
                display(HTML(table_html))
示例#7
0
    def sparql(self, line='', cell='', local_ns: dict = None):
        parser = argparse.ArgumentParser()
        parser.add_argument('query_mode', nargs='?', default='query',
                            help='query mode (default=query) [query|explain]')
        parser.add_argument('--endpoint-prefix', '-e', default='',
                            help='prefix path to sparql endpoint. For example, if "foo/bar" were specified, the endpoint called would be /foo/bar/sparql')
        parser.add_argument('--expand-all', action='store_true')

        request_generator = create_request_generator(self.graph_notebook_config.auth_mode,
                                                     self.graph_notebook_config.iam_credentials_provider_type,
                                                     command='sparql')
        parser.add_argument('--store-to', type=str, default='', help='store query result to this variable')
        args = parser.parse_args(line.split())
        mode = str_to_query_mode(args.query_mode)

        endpoint_prefix = args.endpoint_prefix if args.endpoint_prefix != '' else self.graph_notebook_config.sparql.endpoint_prefix

        tab = widgets.Tab()
        logger.debug(f'using mode={mode}')
        if mode == QueryMode.EXPLAIN:
            res = do_sparql_explain(cell, self.graph_notebook_config.host, self.graph_notebook_config.port,
                                    self.graph_notebook_config.ssl, request_generator, path_prefix=endpoint_prefix)
            store_to_ns(args.store_to, res, local_ns)
            if 'error' in res:
                html = error_template.render(error=json.dumps(res['error'], indent=2))
            else:
                html = sparql_explain_template.render(table=res)
            explain_output = widgets.Output(layout=DEFAULT_LAYOUT)
            with explain_output:
                display(HTML(html))

            tab.children = [explain_output]
            tab.set_title(0, 'Explain')
            display(tab)
        else:
            query_type = get_query_type(cell)
            headers = {} if query_type not in ['SELECT', 'CONSTRUCT', 'DESCRIBE'] else {
                'Accept': 'application/sparql-results+json'}
            res = do_sparql_query(cell, self.graph_notebook_config.host, self.graph_notebook_config.port,
                                  self.graph_notebook_config.ssl, request_generator, headers, endpoint_prefix)
            store_to_ns(args.store_to, res, local_ns)
            titles = []
            children = []

            display(tab)
            table_output = widgets.Output(layout=DEFAULT_LAYOUT)
            # Assign an empty value so we can always display to table output.
            # We will only add it as a tab if the type of query allows it.
            # Because of this, the table_output will only be displayed on the DOM if the query was of type SELECT.
            table_html = ""
            query_type = get_query_type(cell)
            if query_type in ['SELECT', 'CONSTRUCT', 'DESCRIBE']:
                logger.debug('creating sparql network...')

                # some issues with displaying a datatable when not wrapped in an hbox and displayed last
                hbox = widgets.HBox([table_output], layout=DEFAULT_LAYOUT)
                titles.append('Table')
                children.append(hbox)

                expand_all = line == '--expand-all'
                sn = SPARQLNetwork(expand_all=expand_all)
                sn.extract_prefix_declarations_from_query(cell)
                try:
                    sn.add_results(res)
                except ValueError as value_error:
                    logger.debug(value_error)

                logger.debug(f'number of nodes is {len(sn.graph.nodes)}')
                if len(sn.graph.nodes) > 0:
                    f = Force(network=sn, options=self.graph_notebook_vis_options)
                    titles.append('Graph')
                    children.append(f)
                    logger.debug('added sparql network to tabs')

                rows_and_columns = get_rows_and_columns(res)
                if rows_and_columns is not None:
                    table_id = f"table-{str(uuid.uuid4())[:8]}"
                    table_html = sparql_table_template.render(columns=rows_and_columns['columns'],
                                                              rows=rows_and_columns['rows'], guid=table_id)

                # 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.
                if query_type == 'CONSTRUCT' or query_type == 'DESCRIBE':
                    lines = []
                    for b in res['results']['bindings']:
                        lines.append(f'{b["subject"]["value"]}\t{b["predicate"]["value"]}\t{b["object"]["value"]}')
                    raw_output = widgets.Output(layout=DEFAULT_LAYOUT)
                    with raw_output:
                        html = sparql_construct_template.render(lines=lines)
                        display(HTML(html))
                    children.append(raw_output)
                    titles.append('Raw')

            json_output = widgets.Output(layout=DEFAULT_LAYOUT)
            with json_output:
                print(json.dumps(res, indent=2))
            children.append(json_output)
            titles.append('JSON')

            tab.children = children
            for i in range(len(titles)):
                tab.set_title(i, titles[i])

            with table_output:
                display(HTML(table_html))