def generate_graph_html(graph, filename): """ :type graph: networkx.Graph :param str filename: path to save the generated html file """ params = { 'nodes': [], 'links': [], } ids = {} for i, (node, data) in enumerate(graph.nodes(data=True)): val = unicode(node) ids[val] = i params['nodes'].append('{name:"%s",group:%d}' % (node, data.get('group', 1))) for u, v, data in graph.edges(data=True): params['links'].append('{source:%d,target:%d,value:%d,group:%d}' % (ids[unicode(u)], ids[unicode(v)], data.get('weight', 1), data.get('group', 1))) params['nodes'] = ','.join(params['nodes']) params['links'] = ','.join(params['links']) # generating output current_dir = os.path.abspath(os.path.dirname(__file__)) with open( os.path.join(current_dir, 'templates', 'force_directed_graph.html'), 'r') as f: html = f.read() html = render_template(html, params) with open(filename, 'w') as f: f.write(html)
def generate_graph_html(graph, filename): """ :type graph: networkx.Graph :param str filename: path to save the generated html file """ params = { 'nodes': [], 'links': [], } ids = {} for i, (node, data) in enumerate(graph.nodes(data=True)): val = unicode(node) ids[val] = i params['nodes'].append('{name:"%s",group:%d}' % (node, data.get('group', 1))) for u, v, data in graph.edges(data=True): params['links'].append('{source:%d,target:%d,value:%d,group:%d}' % (ids[unicode(u)], ids[unicode(v)], data.get('weight', 1), data.get('group', 1))) params['nodes'] = ','.join(params['nodes']) params['links'] = ','.join(params['links']) # generating output current_dir = os.path.abspath(os.path.dirname(__file__)) with open(os.path.join(current_dir, 'templates', 'force_directed_graph.html'), 'r') as f: html = f.read() html = render_template(html, params) with open(filename, 'w') as f: f.write(html)
'height': 600, 'pname': p.name, 'data': '{}', } if plot_type == 'pie': hnd = LocCounter2() p.rescan_files(hnd) data = hnd.get_data() params['data'] = json.dumps(data) params['max_level'] = 6 params[ 'initial_description'] = '{0} characters, {1} lines of code.'.format( data[1], data[2]) html_template = 'pie_report.html' elif plot_type == 'tree': hnd = LocCounter() p.rescan_files(hnd) params['data'] = json.dumps(hnd.get_data()) html_template = 'tree_report.html' else: print 'Error: Invalid output type.' sys.exit(1) with open(os.path.join(current_dir, 'report_templates', html_template), 'r') as f: html = f.read() html = render_template(html, params) with open(os.path.join(current_dir, 'reports', 'report.html'), 'w') as f: f.write(html)
"lib/debug_toolbar", "static/tiny_mce", "static/js/tiny_mce", "static/admin", ] params = {"width": 600, "height": 600, "pname": p.name, "data": "{}"} if plot_type == "pie": hnd = LocCounter2() p.rescan_files(hnd) data = hnd.get_data() params["data"] = json.dumps(data) params["max_level"] = 6 params["initial_description"] = "{0} characters, {1} lines of code.".format(data[1], data[2]) html_template = "pie_report.html" elif plot_type == "tree": hnd = LocCounter() p.rescan_files(hnd) params["data"] = json.dumps(hnd.get_data()) html_template = "tree_report.html" else: print "Error: Invalid output type." sys.exit(1) with open(os.path.join(current_dir, "report_templates", html_template), "r") as f: html = f.read() html = render_template(html, params) with open(os.path.join(current_dir, "reports", "report.html"), "w") as f: f.write(html)