def dot(graph, simple=True, debug=False, landscape=False): """Format graph as a dot file.""" import sys from rdflib import ConjunctiveGraph from rdflib.plugin import register, Parser from rdflib.tools.rdf2dot import rdf2dot register('json-ld', Parser, 'rdflib_jsonld.parser', 'JsonLDParser') g = ConjunctiveGraph().parse( data=_jsonld(graph, 'expand'), format='json-ld', ) g.bind('prov', 'http://www.w3.org/ns/prov#') g.bind('foaf', 'http://xmlns.com/foaf/0.1/') g.bind('wfdesc', 'http://purl.org/wf4ever/wfdesc#') g.bind('wf', 'http://www.w3.org/2005/01/wf/flow#') g.bind('wfprov', 'http://purl.org/wf4ever/wfprov#') g.bind('schema', 'http://schema.org/') if debug: rdf2dot(g, sys.stdout) return sys.stdout.write('digraph { \n node [ fontname="DejaVu Sans" ] ; \n ') if landscape: sys.stdout.write('rankdir="LR" \n') if simple: _rdf2dot_simple(g, sys.stdout) return _rdf2dot_reduced(g, sys.stdout)
def visualize(g, ofname): stream = io.StringIO() rdf2dot(g, stream, opts={display}) dg = pydotplus.graph_from_dot_data(stream.getvalue()) png = dg.create_png() img = Image(data=png) # generate .png object. open(outputDir + '/' + ofname + '.png', 'wb').write(img.data)
def write_image(self, display=False): """" to do """ with open("%s.dot" % str(self.graph), 'w') as f: rdf2dot(Graph(self.graph), f) if display == "True": Source.main(f)
def dot(graph, simple=True, debug=False, landscape=False, strict=False): """Format graph as a dot file.""" import sys from rdflib.tools.rdf2dot import rdf2dot if strict: raise SHACLValidationError('--strict not supported for json-ld-graph') g = _conjunctive_graph(graph) g.bind('prov', 'http://www.w3.org/ns/prov#') g.bind('foaf', 'http://xmlns.com/foaf/0.1/') g.bind('wfdesc', 'http://purl.org/wf4ever/wfdesc#') g.bind('wf', 'http://www.w3.org/2005/01/wf/flow#') g.bind('wfprov', 'http://purl.org/wf4ever/wfprov#') g.bind('schema', 'http://schema.org/') if debug: rdf2dot(g, sys.stdout) return sys.stdout.write('digraph { \n node [ fontname="DejaVu Sans" ] ; \n ') if landscape: sys.stdout.write('rankdir="LR" \n') if simple: _rdf2dot_simple(g, sys.stdout) return _rdf2dot_reduced(g, sys.stdout)
def describe(events, format='text'): if format == 'text': for event in events: attime = event.get_at_time() beginning = attime.get_beginning() end = attime.get_end() place = event.get_at_place() print( 'A {} event occurred at {} ({}) [{}] on {} starting at {} and ending at {}.' .format(event.get_classification().label, place.get_name(), place.get_country_code(), place.get_location_map(), beginning.get_datetime().strftime('%Y-%m-%d'), beginning.get_datetime().strftime('%H:%M'), end.get_datetime().strftime('%H:%M'))) return if format == 'rdf': g = Graph() g.bind('wgs84', WGS84.ns) g.bind('lode', LODE.ns) g.bind('time', Time.ns) g.bind('geosparql', GeoSPARQL.ns) g.bind('gn', GeoNames.ns) g.bind('sf', SimpleFeatures.ns) g.bind('DUL', DUL.ns) g.bind('smear', SmartSMEAR.ns) for event in events: for s, p, o in event.graph(): g.add((s, p, o)) print(g.serialize(format='turtle', indent=4).decode('utf-8')) return if format == 'graph': rdflib_graph = Graph() rdflib_graph.bind('wgs84', WGS84.ns) rdflib_graph.bind('lode', LODE.ns) rdflib_graph.bind('time', Time.ns) rdflib_graph.bind('geosparql', GeoSPARQL.ns) rdflib_graph.bind('gn', GeoNames.ns) rdflib_graph.bind('sf', SimpleFeatures.ns) rdflib_graph.bind('DUL', DUL.ns) rdflib_graph.bind('smear', SmartSMEAR.ns) for event in events: for s, p, o in event.graph(): rdflib_graph.add((s, p, o)) stream = io.StringIO() rdf2dot(rdflib_graph, stream) (pydot_graph, ) = pydot.graph_from_dot_data(stream.getvalue()) display(SVG(pydot_graph.create_svg())) return raise ValueError('Unsupported format: {}'.format(format))
def graph(): out = StringIO() uri = request.args.get('uri') g = Graph() g.parse(data=json.dumps(es.get(index='bbc', doc_type='item', id=uri)['_source']['jsonld']), format='json-ld') rdf2dot(g, out) dot_graph = graph_from_dot_data(out.getvalue()) return dot_graph.create(format='png'), 200, {'Content-Type': 'image/png'}
def renderGraph(g): """ For rendering an rdflib graph in Jupyter notebooks Args: g: Graph or ConjunctiveGraph Returns: Output for rendering directly in the notebook """ fp = io.StringIO() rdf2dot.rdf2dot(g, fp) return graphviz.Source(fp.getvalue())
def render_graph_dot(g): import tempfile from subprocess import call from os import fork from sys import exit from rdflib.tools.rdf2dot import rdf2dot with tempfile.TemporaryFile() as f: rdf2dot(g, f) f.seek(0, 0) # start of stream g,gname = tempfile.mkstemp() call("dot -T png -o "+gname, stdin = f, shell=True) call("feh "+gname, shell = True)
def graph(): out = StringIO() uri = request.args.get('uri') g = Graph() g.parse(data=json.dumps( es.get(index='bbc', doc_type='item', id=uri)['_source']['jsonld']), format='json-ld') rdf2dot(g, out) dot_graph = graph_from_dot_data(out.getvalue()) return dot_graph.create(format='png'), 200, {'Content-Type': 'image/png'}
def dot(graph): """Format graph as a dot file.""" import sys from rdflib import ConjunctiveGraph from rdflib.plugin import register, Parser from rdflib.tools.rdf2dot import rdf2dot register('json-ld', Parser, 'rdflib_jsonld.parser', 'JsonLDParser') g = ConjunctiveGraph().parse( data=_jsonld(graph, 'expand'), format='json-ld', ) rdf2dot(g, sys.stdout)
def renderGraph(g): """ For rendering an rdflib graph in Jupyter notebooks Args: g (Graph): The graph to render Returns: Jupyter cell: Output for rendering directly in the notebook Example: .. jupyter-execute:: examples/code/eg_rendergraph_01.py """ fp = io.StringIO() rdf2dot.rdf2dot(g, fp) return graphviz.Source(fp.getvalue())
def graph(instance: ConjunctiveGraph) -> str: """ Render RDF graph visually as PNG image. Idea: https://stackoverflow.com/a/61483971/1245471 """ dot_description = io.StringIO() with patch('rdflib.tools.rdf2dot.cgi', html): # FIXME hack, fixes this: https://github.com/RDFLib/rdflib/issues/1110 rdf2dot(instance, dot_description) dg = pydotplus.graph_from_dot_data(dot_description.getvalue()) png = dg.create_png() encoded_png = b64encode(png).decode('utf-8') return f'<img src="data:image/png;base64,{encoded_png}" />'
def make_graphs(rdf_str, fname_base): try: rdf_fname = 'sources/cwms/example_rdfs/' + fname_base + '.rdf' with open(rdf_fname, 'w') as f: f.write(rdf_str) g_rdf = rdflib.Graph() g_rdf.parse(rdf_fname, publicID=fname_base) dot_fname = 'sources/cwms/example_dots/' + fname_base + '.dot' with open(dot_fname, 'w') as f: rdf2dot.rdf2dot(g_rdf, f) if HAVE_PYDOT: g_dot = pydot.graph_from_dot_file(dot_fname)[0] logger.warning("pydot is not available, so pydot graph was not " "produced. To get pydot, run `pip install pydot`.") else: g_dot = None except Exception as e: logger.error("Got exeption for rdf string: %s" % rdf_str) logger.exception(e) return (None, None) return (g_rdf, g_dot)
def graphrdf(graph, format_): return dot(lambda uw: rdf2dot.rdf2dot(graph, stream=uw), format_)
def get_rdf_graph(graph, format_): return dot(lambda uw: rdf2dot.rdf2dot(graph, stream=uw), format_)
def graphrdf(graph, format_): return dot(lambda uw: rdf2dot.rdf2dot( graph, stream=uw), format_)