I am trying to load my files that I have defined on a JavaScript file (with D3-Graphviz) to a Flask server. Here is my current server structure:
static
--images
--File.png
--Actor.png
--System.png
--Service.png
--background.jpg
--style.css
--visualisation.js
templates
--website.html
Here is my current code to implement the graph (with icons) in visualisation.js
:
let graph = d3.select("#graph")
let newgraphviz = graph.graphviz()
newgraphviz.transition(function () {
return d3.transition("main")
.ease(d3.easeLinear)
.delay(500)
.duration(1500);}
)
.on("initEnd", render)
.on("end", draw_edges);
let graph2 = graph.selectAll(".edge").nodes()
console.log(graph2)
function draw_edges() {
graph.selectAll(".edge")
.nodes()
.forEach(function (e, i) {
setTimeout(() => {
{
let ee = d3.select(e);
ee.select("path")
.attr("stroke-width", 1)
.transition()
.duration(800)
.attr("stroke", "red")
.attr("stroke-width", 2);
ee.select("polygon").transition().duration(800).attr("fill", "red");
}
}, 800 * (i + 1));
});
}
function renderString(str){
newgraphviz.tweenShapes(false)
.addImage("{{ url_for('static', filename='images/Actor.png') }}", "50px", "50px")
.addImage("{{ url_for('static', filename='images/File.png') }}", "50px", "50px")
.addImage("{{ url_for('static', filename='images/System.png') }}", "50px", "50px")
.addImage("{{ url_for('static', filename='images/Service.png') }}", "50px", "50px")
.renderDot(str, function() {
graph.selectAll("title").remove();
graph.selectAll("text").style("pointer-events", "none");
});
}
// This would be used to render the GV file.
function render(filename) {
fetch(filename).then(response => response.text()).then(textAsString =>
renderString(textAsString));
}
And here is my code (in Flask) for deploying the website:
from flask import Flask, render_template, request, jsonify
import subprocess
app = Flask(__name__, static_url_path='/static')
@app.route('/')
def index():
return render_template('website.html')
if __name__ == '__main__':
app.run(debug=True)
However, when I run this Python file to run the website.html
file, I could not get the icons (File.png
, Actor.png
, System.png
and Service.png
) displayed in the graph, although my background.png
file (that I defined in the CSS file) could still be loaded.
So, is the problem in my code related to D3-Graphviz, or just a problem about loading images in Flask? And what should I do to resolve this problem?
Update: From what I read in Flask, I have changed the addImage function to .addImage("{{ url_for('static', filename='images/System.png') }}", "50px", "50px")
, however this does not seem to solve my problem.