SimpleHTTPServer代码404,未找到消息文件

2024-04-28 04:04:48 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图运行一个简单的d3 Javascript程序来可视化一个图形。我还有一个用于这个图的JSON文件。为了让程序运行,我被告知应该遵循以下步骤:

1-在终端上,我转到项目所在的文件夹。
2-我插入以下命令:python -m SimpleHTTPServer 8888 &
3-在Web浏览器(Firefox)上,我添加了这个:http://localhost:8888

当我执行第三步时,终端显示以下错误消息:

localhost - - [11/Nov/2013 08:07:23] code 404, message File not found
localhost - - [11/Nov/2013 08:07:23] "GET /D3/sample.json HTTP/1.1" 404 -

这是我的d3 Javascript图形的HTML文件:

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
</head>
<style>

.node {
   stroke: #fff;
   stroke-width: 1.5px;
}

.link {
   stroke: #999;
   stroke-opacity: .6; 
}

</style> 
<body>
<p> Paragraph !!! </p>
<script type="text/javascript" src="d3.v3.js"></script>
<script>

var width = 960,
height = 500;

var color = d3.scale.category20();

var force = d3.layout.force()
.charge(-120)
.linkDistance(30)
.size([width, height]);

var svg = d3.select("body").append("svg")
  .attr("width", width)
  .attr("height", height);

d3.json("sample.json", function(error, graph) {
force
  .nodes(graph.nodes)
  .links(graph.links)
  .start();

var link = svg.selectAll(".link")
  .data(graph.links)
  .enter().append("line")
  .attr("class", "link")
  .style("stroke-width", function(d) { return Math.sqrt(d.value); });

var node = svg.selectAll(".node")
  .data(graph.nodes)
  .enter().append("circle")
  .attr("class", "node")
  .attr("r", 5)
  .style("fill", function(d) { return color(d.group); })
  .call(force.drag);

node.append("title")
  .text(function(d) { return d.name; });

force.on("tick", function() {
   link.attr("x1", function(d) { return d.source.x; })
   .attr("y1", function(d) { return d.source.y; })
   .attr("x2", function(d) { return d.target.x; })
   .attr("y2", function(d) { return d.target.y; });

node.attr("cx", function(d) { return d.x; })
   .attr("cy", function(d) { return d.y; });
});
});

</script>
</body>
</html>

JSON文件sample.json似乎不能像上面显示的那样读取。有谁能帮助我如何让程序运行,并获得json文件读取使用我提供的上述命令。如果我在HTML文件中添加了一个标题和段落,它们将出现,但无法显示图形。JSON文件的位置有问题吗?或者d3.v3.js文件有问题吗?谢谢你的帮助。

`


Tags: 文件nodejsonreturnstrokestylevarlink
1条回答
网友
1楼 · 发布于 2024-04-28 04:04:48

据我所知,您已经在一个目录中设置了一个python简单服务器,并且在该目录中有一个html文件,该文件显示在浏览器中。但是,当您尝试运行js代码并加载json文件时,会出现404错误。

错误是它在名为D3的目录中查找json文件,但是,您的代码在根目录中查找json。尝试更改

D3.json("sample.json", function(error, graph)

行至

d3.json("D3/sample.json", function(error, graph)

另外,在函数调用位置console.log(graph)内,如下所示:

d3.json("sample.json", function(error, graph) {
    console.log(graph)

这会将输出发送到您的控制台,以便您可以检查正在读取的内容(如果您已经知道,请道歉)。

相关问题 更多 >