async def websocket_endpoint(title: str, websocket: WebSocket): title = title.replace("_", " ") database = Database(sslmode=False) with database.Session() as sess: result = sess.query(WikiMap).filter_by(title=title, lpp=12).first() logging.info(result) graph = WikipediaGraph("Elon Musk", levels=3, lpp=12) await websocket.accept() steps = 0 delay = [1.5, 1, 0.75] for json_chunk in graph.generate_from_wikimap(result, yield_size=15): await websocket.send_json(json_chunk) if steps < 3: await asyncio.sleep(delay[0]) elif steps < 5: await asyncio.sleep(delay[1]) else: await asyncio.sleep(delay[2]) steps += 1 await websocket.close(code=1000)
def write_json(self): """write graph to file use the json format that 3d-force-directed-graph can read """ graph_json = self.get_json_dict() page_name = self.start_page.replace(" ", "_") file_name = "static/sample_data/{}_l_{}_lpp_{}.json".format( page_name, self.levels, self.lpp) with open(file_name, "w") as f: json.dump(graph_json, f) logging.info("Wrote file %s", file_name) if __name__ == "__main__": # graph = WikipediaGraph("Neuroscience", levels=4, lpp=8) # for json_chunk in graph.generate(): # pass # # print(json.dumps(json_chunk, indent=2, separators=(",", ": "))) # graph.write() from database.db import Database database = Database(sslmode=False) with database.Session() as sess: result = sess.query(WikiMap).filter_by(title="COVID-19 pandemic", lpp=2).first() logging.info(result) graph = WikipediaGraph("COVID-19 pandemic", levels=2, lpp=2) for json_chunk in graph.generate_from_wikimap(result, yield_size=5): print(json_chunk)