在从javascript / html

时间:2017-09-26 17:11:06

标签: javascript python html csv webpage

我有一个index.html,它读取csv文件并使用Javascript DataTables在网页上显示。它如下:

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->
    <title>CSV to HTML Table</title>

    <!-- Bootstrap core CSS -->
    <link href="css/bootstrap.min.css" rel="stylesheet">
    <link href="css/dataTables.bootstrap.css" rel="stylesheet">

    <!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
    <!--[if lt IE 9]>
      <script type="text/javascript" src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
      <script type="text/javascript" src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->
  </head>

  <body>
    <div class="container-fluid">

      <h2>CSV to HTML Table</h2>



      <div id='table-container'></div>

    </div><!-- /.container -->



    <!-- Bootstrap core JavaScript
    ================================================== -->
    <!-- Placed at the end of the document so the pages load faster -->
    <script type="text/javascript" src="js/jquery.min.js"></script>
    <script type="text/javascript" src="js/bootstrap.min.js"></script>
    <script type="text/javascript" src="js/jquery.csv.min.js"></script>
    <script type="text/javascript" src="js/jquery.dataTables.min.js"></script>
    <script type="text/javascript" src="js/dataTables.bootstrap.js"></script>
    <script type="text/javascript" src="js/csv_to_html_table.js"></script>


    <script type="text/javascript">
      function format_link(link){
        if (link)
          return "<a href='" + link + "' target='_blank'>" + link + "</a>";
        else
          return "";
      }
      CsvToHtmlTable.init({
        csv_path: 'data/fatty_acid_profiles.csv',
        element: 'table-container', 
        allow_download: true,
        csv_options: {separator: ','},
        datatables_options: {"paging": false},
        custom_formatting: [[4, format_link]]
      });
    </script>
  </body>
</html>

我在ec2实例上运行此操作。我希望在打开此URL时,在加载页面之前,首先应运行一个名为computation.py的python脚本,创建上面的data/fatty_acid_profiles.csv文件然后加载页面。如何在加载页面之前启用运行python脚本?

1 个答案:

答案 0 :(得分:0)

您可以创建一个简单的烧瓶应用来提供HTML文件,请参考Flask Documentation。在索引路由中,您可以调用calculate.py的函数来生成所需的CSV文件,然后返回HTML页面。