【发布时间】:2020-08-13 00:27:47
【问题描述】:
概述: 我正在使用 Python 创建一个网页,并在我的代码中生成 html 和 javascript。此外,我正在解析 csv 文件并将它们的表格数据转换为 html。我希望能够单击一行文本,然后将该文本的关联表格数据加载到当前活动网页上的 iframe 中。我遇到的问题是我的 javascript 函数无法识别我发送它以检索相应表数据的密钥。如果我手动输入返回表数据的密钥,则会返回正确的数据——尽管表没有加载。但是,如果我以编程方式生成密钥,即使字符串看起来相同,它也会返回为“未定义”。
目标: 我需要弄清楚我用来尝试检索表数据的语法或键的格式是否有问题。其次,我需要弄清楚为什么表格数据没有正确加载到我的 iframe 中。
例子:
import pandas
opening_html = """<!DOCTYPE html><h1> Test</h1><div style="float:left">"""
table_html = pandas.DataFrame({'Col_1':['this', 'is', 'a', 'test']}).to_html()
tables_dict = {'test-1 00': table_html}
java_variables = "%s" % json.dumps(tables_dict)
table_frame = """<iframe name="table_frame" style="position:fixed; top:100px; width:750; height:450"></iframe>"""
test_link_text = """<a href='' onclick="send_table_data(this);"> test-1</a><br>"""
java = """<script type='text/javascript'>
var table_filename = """ + java_variables + ";"
java += """function send_table_data(obj) {
var t = obj.text + ' 00';
alert(t)
//This line below will not work
var table_data = table_filename[t];
//But this line will return the correct value
var table_data = table_filename['test-1 00'];
alert(table_data);
//This line should load the data, but does nothing
document.getElementsByName('table_frame').src = table_data;
}
</script>"""
html_text = """<head>
<link rel="stylesheet" href="style.css">
</head>""" + test_link_text + table_frame + """<body>""" + "</div>" + java + '</body>'
with open('test_table_load.html', 'w') as w:
w.write(html_text)
编辑:我刚刚发现由于某种原因在 var t 的开头有一个默认空格 - 所以使用 trim() 似乎可以解决这个问题。现在,剩下的唯一问题是为什么数据没有加载到表中。
【问题讨论】:
-
要摆脱“默认空间”,只需从 ">test-1
" 中删除空间。
标签: javascript python iframe