gpt4 book ai didi

javascript - HTML 在单击时显示图形,但在 flask 中访问时不显示图形

转载 作者:行者123 更新时间:2023-12-01 06:33:32 24 4
gpt4 key购买 nike

我对 Flask 完全陌生,所以如果问题愚蠢,请原谅我

我正在学习如何使用flask并希望显示由sigma.js生成的图表。

当我通过双击文件直接打开“svg-freestyle-renderer.html”时,它会打开并显示带有节点和连接的可交互网络图。

现在我想通过访问 Flask 服务器中的 html 来查看相同的图表,但是当我打开页面时,它只加载背景颜色而不加载图表。我已经三次检查我的所有文件是否都在正确的目录中。

直接打开 html 文件和通过我缺少的 Flask 访问它有区别吗?

这就是我在 py 文件中调用 html 的方式:

@app.route("/graph")
def draw_graph():
return render_template('svg-freestyle-renderer.html')

这是 .html:

<html>

<body>

<!-- START SIGMA IMPORTS -->
<script src="../src/sigma.core.js"></script>
<script src="../src/conrad.js"></script>
<script src="../src/utils/sigma.utils.js"></script>
<script src="../src/utils/sigma.polyfills.js"></script>
<script src="../src/sigma.settings.js"></script>
<script src="../src/classes/sigma.classes.dispatcher.js"></script>
<script src="../src/classes/sigma.classes.configurable.js"></script>
<script src="../src/classes/sigma.classes.graph.js"></script>
<script src="../src/classes/sigma.classes.camera.js"></script>
<script src="../src/classes/sigma.classes.quad.js"></script>
<script src="../src/classes/sigma.classes.edgequad.js"></script>
<script src="../src/captors/sigma.captors.mouse.js"></script>
<script src="../src/captors/sigma.captors.touch.js"></script>
<script src="../src/renderers/sigma.renderers.canvas.js"></script>
<script src="../src/renderers/sigma.renderers.webgl.js"></script>
<script src="../src/renderers/sigma.renderers.svg.js"></script>
<script src="../src/renderers/sigma.renderers.def.js"></script>
<script src="../src/renderers/webgl/sigma.webgl.nodes.def.js"></script>
<script src="../src/renderers/webgl/sigma.webgl.nodes.fast.js"></script>
<script src="../src/renderers/webgl/sigma.webgl.edges.def.js"></script>
<script src="../src/renderers/webgl/sigma.webgl.edges.fast.js"></script>
<script src="../src/renderers/webgl/sigma.webgl.edges.arrow.js"></script>
<script src="../src/renderers/canvas/sigma.canvas.labels.def.js"></script>
<script src="../src/renderers/canvas/sigma.canvas.hovers.def.js"></script>
<script src="../src/renderers/canvas/sigma.canvas.nodes.def.js"></script>
<script src="../src/renderers/canvas/sigma.canvas.edges.def.js"></script>
<script src="../src/renderers/canvas/sigma.canvas.edges.curve.js"></script>
<script src="../src/renderers/canvas/sigma.canvas.edges.arrow.js"></script>
<script src="../src/renderers/canvas/sigma.canvas.edges.curvedArrow.js"></script>
<script src="../src/renderers/canvas/sigma.canvas.edgehovers.def.js"></script>
<script src="../src/renderers/canvas/sigma.canvas.edgehovers.curve.js"></script>
<script src="../src/renderers/canvas/sigma.canvas.edgehovers.arrow.js"></script>
<script src="../src/renderers/canvas/sigma.canvas.edgehovers.curvedArrow.js"></script>
<script src="../src/renderers/canvas/sigma.canvas.extremities.def.js"></script>
<script src="../src/renderers/svg/sigma.svg.utils.js"></script>
<script src="../src/renderers/svg/sigma.svg.nodes.def.js"></script>
<script src="../src/renderers/svg/sigma.svg.edges.def.js"></script>
<script src="../src/renderers/svg/sigma.svg.edges.curve.js"></script>
<script src="../src/renderers/svg/sigma.svg.labels.def.js"></script>
<script src="../src/renderers/svg/sigma.svg.hovers.def.js"></script>
<script src="../src/middlewares/sigma.middlewares.rescale.js"></script>
<script src="../src/middlewares/sigma.middlewares.copy.js"></script>
<script src="../src/misc/sigma.misc.animation.js"></script>
<script src="../src/misc/sigma.misc.bindEvents.js"></script>
<script src="../src/misc/sigma.misc.bindDOMEvents.js"></script>
<script src="../src/misc/sigma.misc.drawHovers.js"></script>
<!-- END SIGMA IMPORTS -->
<script src="../plugins/sigma.plugins.neighborhoods/sigma.plugins.neighborhoods.js"></script>
<script src="../plugins/sigma.layout.forceAtlas2/supervisor.js"></script>
<script src="../plugins/sigma.layout.forceAtlas2/worker.js"></script>
<script src="lib/jquery-2.1.1.min.js"></script>

<div id="container">
<style>
#graph-container {
top: 0;
bottom: 0;
left: 0;
right: 0;
position: absolute;
background-color: #455660;
}
.sigma-edge {
stroke: #14191C;
}
.sigma-node {
fill: green;
stroke: #14191C;
stroke-width: 2px;
}
.sigma-node:hover {
fill: blue;
}
.muted {
fill-opacity: 0.1;
stroke-opacity: 0.1;
}
</style>
<div id="graph-container"></div>
</div>

<script>
/**
* This is a basic example of how one could spawn a freestyle svg renderer
* to achieve his/her goal through css and jQuery to display fancy graphs
* but somewhat less performant.
*/
var i,
s,
N = 100,
E = 500,
g = {
nodes: [],
edges: []
};

// Generate a random graph:
for (i = 0; i < N; i++)
g.nodes.push({
id: 'n' + i,
label: 'Node ' + i,
x: Math.random(),
y: Math.random(),
size: Math.random(),
color: '#666'
});

for (i = 0; i < E; i++)
g.edges.push({
id: 'e' + i,
source: 'n' + (Math.random() * N | 0),
target: 'n' + (Math.random() * N | 0),
size: Math.random(),
color: '#ccc'
});

// Instantiate sigma:
s = new sigma({
graph: g,
settings: {
enableHovering: false
}
});

s.addRenderer({
id: 'main',
type: 'svg',
container: document.getElementById('graph-container'),
freeStyle: true
});

s.refresh();

// Binding silly interactions
function mute(node) {
if (!~node.getAttribute('class').search(/muted/))
node.setAttributeNS(null, 'class', node.getAttribute('class') + ' muted');
}

function unmute(node) {
node.setAttributeNS(null, 'class', node.getAttribute('class').replace(/(\s|^)muted(\s|$)/g, '$2'));
}

$('.sigma-node').click(function() {

// Muting
$('.sigma-node, .sigma-edge').each(function() {
mute(this);
});

// Unmuting neighbors
var neighbors = s.graph.neighborhood($(this).attr('data-node-id'));
neighbors.nodes.forEach(function(node) {
unmute($('[data-node-id="' + node.id + '"]')[0]);
});

neighbors.edges.forEach(function(edge) {
unmute($('[data-edge-id="' + edge.id + '"]')[0]);
});
});

s.bind('clickStage', function() {
$('.sigma-node, .sigma-edge').each(function() {
unmute(this);
});
});
</script>

</body>
</html>

谢谢!

最佳答案

在浏览器中右键单击您的页面,然后单击“检查元素”并打开“控制台”。你会看到类似的东西

Failed to load resource: the server responded with a status of 404 (NOT FOUND)

适用于您的所有文件。

将您的 js 文件放在名为 static 的目录中,或者您可以指定自己创建应用程序 app = Flask(static_folder="src")

并在您的模板中使用 {{ url_for('static', filename='sigma.core.js') }} 访问您的 js 文件。您将无法直接从文件系统在浏览器中打开此页面,但应用程序可以完美地提供该页面。

关于static files的链接和 app API供引用。

关于javascript - HTML 在单击时显示图形,但在 flask 中访问时不显示图形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59780559/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com