gpt4 book ai didi

javascript - 上传包含复数的 CSV 文件并在 Bokeh 网络应用程序中读取它

转载 作者:行者123 更新时间:2023-11-30 06:15:14 25 4
gpt4 key购买 nike

我想将包含复数的 csv 文件上传到 bokeh 网络应用程序中。 DuCorey 的解决方案非常适合整数和 float ,请参阅:Upload a CSV file and read it in Bokeh Web app但是当我在 models.py 中将 values_type 从 Int 更改为 Complex 并将复数插入 csv 文件时,它不再起作用。我不想像 DuCorey 那样绘制导入的数组(这在他的示例中不起作用)。只需导入值和 f.e.打印它们。

我更改了以下三个文件:

模型.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('Complex expected Complex, got 5+1j').

谢谢大家的建议!

最佳答案

另一个示例中使用的方法使用 JavaScript 库来解析文件,并将其发回。 AFAIK 这在这种情况下不起作用,原因有二:

  • papaparse JS 库本身对解析复数一无所知
  • 我不确定将复杂值从 JS 发送回 Python 是否可行。据我所知,自 2013 年以来,您是第一个想要尝试的人

我认为要使这项工作最好的办法是简单地将原始文件的全部内容发送回 Bokeh 服务器(作为一个大的 String),然后使用 Pandas 从中解析文件字符串。

关于javascript - 上传包含复数的 CSV 文件并在 Bokeh 网络应用程序中读取它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56565562/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com