我想将具有复数的csv文件上传到bokeh网络应用中。 DuCorey的解决方案非常适合整数和浮点数,请参见:Upload a CSV file and read it in Bokeh Web app 但是,当我在models.py中将values_type从Int更改为Complex并将一个复数插入csv文件时,它不再起作用。我不想像DuCorey那样绘制导入的数组(这在他的示例中不起作用)。只需导入值并执行打印它们。
我更改了以下三个文件:
models.py
from bokeh.core.properties import List, String, Dict, Int, Complex, Float
from bokeh.models import LayoutDOM
class FileInput(LayoutDOM):
__implementation__ = 'static/js/extensions_file_input.coffee'
__javascript__ = './input_widget/static/js/papaparse.js'
value = String(help="""
Selected input file.
""")
file_name = String(help="""
Name of the input file.
""")
accept = String(help="""
Character string of accepted file types for the input. This should be
written like normal html.
""")
data = List(Dict(keys_type=String, values_type=Complex))
csv文件
x,y
1,2
3,3
3,5+1j
10,25
和main.py
from bokeh.core.properties import List, String, Dict, Int
from bokeh.models import LayoutDOM
from bokeh.layouts import column
from bokeh.models import Button, ColumnDataSource
from bokeh.io import curdoc
from bokeh.plotting import Figure
import pandas as pd
from models import FileInput
button_input = FileInput(id="fileSelect",
accept=".csv")
def change_plot_data(attr, old, new):
new_df = pd.DataFrame(new)
print(new_df)
button_input.on_change('data', change_plot_data)
layout = column(button_input)
curdoc().add_root(layout)
我收到以下错误消息: DeserializationError('复杂的预期复杂度,得到5 + 1j')。
感谢所有建议!
答案 0 :(得分:0)
在另一个示例中使用的方法使用JavaScript库进行文件解析,然后将其发送回去。 AFAIK在这种情况下将无法使用,原因有两个:
papaparse
JS库本身对解析复数一无所知我认为最好的选择是将原始文件的全部内容发送回Bokeh服务器(作为一个大String
),然后使用Pandas从字符串中解析文件。