express和python从csv渲染html表"/>
[使用node.js,express和python从csv渲染html表
我有一个简单的csv文件,并希望使用express和ejs从中渲染一个html表。
csv:
,Unnamed: 0,sign_type,r1,g1,b1,r2,g2,b2,r3,g3,b3,r4,g4,b4,r5,g5,b5,r6,g6,b6,r7,g7,b7,r8,g8,b8,r9,g9,b9,r10,g10,b10,r11,g11,b11,r12,g12,b12,r13,g13,b13,r14,g14,b14,r15,g15,b15,r16,g16,b16
0,1,pedestrian,155,228,251,135,188,101,156,227,245,145,211,228,166,233,245,212,254,52,212,254,11,188,229,117,170,216,120,211,254,3,212,254,19,172,235,244,172,235,244,172,228,235,177,235,244,22,52,53
1,2,pedestrian,142,217,242,166,204,44,142,217,242,147,219,242,164,228,229,84,116,17,217,254,26,155,203,128,213,253,51,217,255,21,217,255,21,158,225,237,164,227,237,182,228,143,171,228,196,164,227,237
2,3,pedestrian,57,54,50,187,201,68,51,51,45,59,62,65,156,171,50,254,255,36,211,226,70,78,73,64,220,234,59,254,255,51,253,255,44,66,68,68,69,65,59,76,84,22,82,93,17,58,60,60
3,4,pedestrian,22,35,41,171,178,26,19,27,29,19,27,29,42,37,3,217,228,19,221,235,20,181,183,73,237,234,44,251,254,2,235,243,12,19,27,29,20,29,34,64,61,4,211,222,78,19,27,29
4,5,pedestrian,169,179,170,231,254,27,97,107,99,123,147,152,221,236,117,205,225,80,235,254,60,90,110,9,216,236,66,229,255,12,235,254,60,163,168,152,124,117,91,188,205,78,125,147,20,160,183,187
我的.html文件如下:
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title>Table</title>
</head>
<body>
<%= table %>
</body>
</html>
我的app.js是:
const express = require('express');
const app = express();
const bodyParser = require('body-parser');
let {PythonShell} = require('python-shell');
app.set('view engine', 'ejs');
app.get('/', function(req, res){
var pyshell = new PythonShell('excel_to_html.py');
pyshell.on('message', function (message) {
res.render('list', {table: message}) // ###### console.log(message);
});
});
var port = 3000;
app.listen(port, function(){
console.log('Server running on port ' + port)
});
并且将.csv转换为html的python文件是:
import pandas as pd
def convert():
csv_file = pd.read_csv('test.csv')
html = csv_file.to_html()
return html
if __name__ == '__main__':
var = convert()
print(var)
因此总结:
1)我有一个csv文件,并使用.py文件和熊猫将csv转换为html。
2)我正在app.js文件中调用python脚本并获取返回的html。
3)我想使用ejs在index.html中呈现该html表。
如果将res.render('list', {table: message})
替换为带有注释的部分console.log(message);
,则可以在终端日志中看到html表。
但是当我尝试使用res.render('list', {table: message})
渲染它时,出现了这样的一个非无限循环错误:
Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
at ServerResponse.setHeader (_http_outgoing.js:485:11)
at ServerResponse.header (E:\Documents\CODE\WebDev\todolist\v1\node_modules\express\lib\response.js:771:10)
at ServerResponse.send (E:\Documents\CODE\WebDev\todolist\v1\node_modules\express\lib\response.js:170:12)
at done (E:\Documents\CODE\WebDev\todolist\v1\node_modules\express\lib\response.js:1008:10)
at tryHandleCache (E:\Documents\CODE\WebDev\todolist\v1\node_modules\ejs\lib\ejs.js:260:5)
at View.exports.renderFile [as engine] (E:\Documents\CODE\WebDev\todolist\v1\node_modules\ejs\lib\ejs.js:459:10)
at View.render (E:\Documents\CODE\WebDev\todolist\v1\node_modules\express\lib\view.js:135:8)
at tryRender (E:\Documents\CODE\WebDev\todolist\v1\node_modules\express\lib\application.js:640:10)
at Function.render (E:\Documents\CODE\WebDev\todolist\v1\node_modules\express\lib\application.js:592:3)
at ServerResponse.render (E:\Documents\CODE\WebDev\todolist\v1\node_modules\express\lib\response.js:1012:7)
而且我在浏览器中只得到这个:
<table border="1" class="dataframe">
我应该如何渲染此表?有可能吗?
谢谢!
回答如下:Basically Express的res.render结束响应;这意味着我们只能使用一次,并且似乎监听器pyshell.on('message'...
被多次触发。
您需要准备表格然后进行渲染。示例:
const express = require('express');
const app = express();
const { PythonShell } = require('python-shell');
app.set('view engine', 'ejs');
app.get('/', function(req, res){
prepareHtmlTable(function(err, html) {
if (err) return res.sendStatus(500);
res.render('list', { table: html });
})
});
function prepareHtmlTable(callback) {
const pyshell = new PythonShell('excel_to_html.py');
pyshell.on('message', function (message) {
// prepare html table here
let html = message;
pyshell.end(function (err,code,signal) {
if (err) return callback(err);
callback(null, html);
});
});
}
const port = 3000;
app.listen(port, function(){
console.log('Server running on port ' + port)
});
更多推荐
[使用node.js,express和python从csv渲染html表
发布评论