This is how I save the nodes and edges of a sigma graph into a JSON file. The JSON file can be later re-imported with the sigma.parsers.json plugin.
<button id="export-btn">Save Graph On Server</button><br/>
$('#export-btn').click(function(event) {
var filename = prompt("What do you want to name the saved graph?");
data = {
'graph': {
'nodes': s.graph.nodes(),
'edges': s.graph.edges()
},
'filename': filename
}
$.ajax({
type: 'post',
url: '/save-graph',
data: JSON.stringify(data),
dataType: "json",
contentType: "application/json; charset=utf-8",
});
});
You probably want to use secure_filename
@app.route('/save-graph', methods=['POST'])
def save_graph():
data = json.loads(request.data)
graph_data = data['graph']
filename = data['filename']
if not filename.endswith('.json'):
filename += '.json'
with open('viz/data/' + filename, 'w', encoding='utf-8') as file:
file.write(json.dumps(graph_data, indent=2))
return 'Success'