【问题标题】:HTML Page cannot view using FLASK - got 404 errorHTML 页面无法使用 FLASK 查看 - 出现 404 错误
【发布时间】:2020-01-16 03:59:35
【问题描述】:

我可以使用烧瓶浏览到我的主页 index.html 页面,但是当我单击索引页面上的按钮时,页面被定向到 mytopo.html 页面,但没有出现拓扑视图并出现错误。尝试转到 mytopo.html 页面时出现以下错误。似乎找不到我的 json 文件(mytopo.json)

192.168.56.1 - - [16/Jan/2020 11:31:15] "GET /mytopo HTTP/1.1" 200 -
192.168.56.1 - - [16/Jan/2020 11:31:15] "GET /mytopo.json HTTP/1.1" 404 -

这是flask项目的结构

mywebfolder
  myapp.py --- main file and run server
  static --- for images
   image1.jpg
  templates --- all html files are here
   index.html
   mytopo.html
  data --- my json file is store here
   mytopo.json

myapp.py

app = Flask(__name__)

#Function for index page
@app.route('/')
def startPage():
    bodyText=Markup("<b>MAIN PAGE</b>")
    return render_template('index.html', bodyText=bodyText)

#topology view
@app.route('/mytopo')
def mytopo():
    return render_template("mytopo.html")

index.html

<button style="width:10px;height:10px"><font size="6"><a href="mytopo">Topology View</a></font></button>

mytopo.html是使用vis.js和json文件(mytopo.json)查看拓扑

var json = $.getJSON("mytopo.json")
  .done(function(data){
    var data = {
      nodes: data.nodes,
      edges: data.edges
    };
    var network = new vis.Network(container, data, options);
  });

var container = document.getElementById('mynetwork');

</script>

我真的很感激有人能带我走……请帮忙,谢谢。

【问题讨论】:

    标签: python html json flask vis.js


    【解决方案1】:

    简而言之,您没有提供 JSON 文件的路径。在您的情况下,您必须创建一个新的视图函数来使用 URL 规则 /mytopo.json 提供您的 JSON 数据:

    from flask import send_from_directory
    
    @app.route('/mytopo.json')
    def mytopo_json():
        return send_from_directory(app.root_path, 'data/mytopo.json') 
    

    (代码尚未测试,如果不起作用,请告诉我)

    【讨论】:

    • 嗨...我会测试它并更新结果...谢谢
    猜你喜欢
    • 2017-11-04
    • 1970-01-01
    • 2017-07-14
    • 2020-09-09
    • 1970-01-01
    • 2023-03-11
    • 1970-01-01
    • 1970-01-01
    • 2017-01-23
    相关资源
    最近更新 更多