将文件信息从html文件选择器输入传递给python和bokeh [英] Pass file information from html file selector input to python and bokeh

查看:141
本文介绍了将文件信息从html文件选择器输入传递给python和bokeh的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图创建一个简单的散景服务器应用程序,允许用户从< input type =file> 文件选择按钮加载文件。然后,应用程序将绘制用户选择的文件中的数据。下面的代码非常简单,我只是不知道如何将文件信息从文件选择器传递给python。我需要使用python来处理文件I / O,而不是html或javascript。

我运行时可以很好地工作,散景服务 - 在命令行显示example.py路径/到/ input_file ,但我不希望用户每次都指定它。我需要他们能够点击一个按钮来上传文件。这个应用程序在本地运行,所以没有上传到服务器或类似的东西。



是否有比< input type更好的方法=file>

  from bokeh.plotting导入图b $ b from bokeh .layouts从bokeh.models导入布局
从bokeh.io导入ColumnDataSource,Div
导入curdoc

desc = Div(text =
< h1> ;一个简单的例子< / h1>
< input type =file>
< br />,width = 800)

#Create列数据源将被绘图使用
source = ColumnDataSource(data = dict(x = [],y = []))

p = figure(plot_height = 550,plot_width = 800,title =,toolbar_location ='above')
p.line(x =x,y =y,source = source)

def update
x_data,y_data = read_file_data(input_file_name)#读取特定文件类型的函数
source.data = dict(
x = x_data,
y = y_data,


sizing_mode =固定的'#'scale_width'在这个例子中也很好看
l = layout([
[desc],
[p],
],sizing_mode = sizing_mode)

update()
curdoc()。add_root(l)
curdoc()。title =Sample


解决方案

从Bokeh 0.12.4 ,没有内置文件选择器输入窗口小部件。但可以作为继续的最佳地点。

I'm trying to create a simple bokeh server application that allows a user to load a file from a <input type="file"> file selection button. The app will then plot the data from the file that the user selected. The code below is very simplistic, and I simply don't know how to pass the file information from the file selector to python. I need to use python to handle the file I/O and not html or javascript.

I can get it to work just fine when I run bokeh serve --show example.py path/to/input_file at the command line, but I don't want the user to specify this each time. I need them to be able to click a button to "upload" the file. This application is running locally, so there is no uploading to a server or anything like that.

Is there a better method than <input type="file"> ?

from bokeh.plotting import figure
from bokeh.layouts import layout
from bokeh.models import ColumnDataSource, Div
from bokeh.io import curdoc

desc = Div(text="""
<h1>A simple example</h1>
<input type="file">
<br />""", width=800)

# Create Column Data Source that will be used by the plot
source = ColumnDataSource(data=dict(x=[], y=[]))

p = figure(plot_height=550, plot_width=800, title="", toolbar_location='above')
p.line(x="x", y="y", source=source)

def update():
    x_data,y_data = read_file_data(input_file_name) # function to read specific file type
    source.data = dict(
        x=x_data,
        y=y_data,
    )

sizing_mode = 'fixed'  # 'scale_width' also looks nice with this example
l = layout([
    [desc],
    [p],
], sizing_mode=sizing_mode)

update()
curdoc().add_root(l)
curdoc().title = "Sample"

解决方案

As of Bokeh 0.12.4, there's no built-in file chooser input widget. But it is possible to create new extensions to Bokeh that work as seamlessly as the built-in widgets to connect JS events to Python.

The code below is a super-rough implementation of a model that wraps an <input type="file"> to hook it up to Python code. This code should work with Bokeh 0.12.4 and newer.

from bokeh.core.properties import String
from bokeh.io import curdoc
from bokeh.layouts import column
from bokeh.models import Button, LayoutDOM

IMPL = """
import * as p from "core/properties"
import {LayoutDOM, LayoutDOMView} from "models/layouts/layout_dom"

export class FileInputView extends LayoutDOMView
  initialize: (options) ->
    super(options)
    input = document.createElement("input")
    input.type = "file"
    input.onchange = () =>
      @model.value = input.value
    @el.appendChild(input)

export class FileInput extends LayoutDOM
  default_view: FileInputView
  type: "FileInput"
  @define {
    value: [ p.String ]
  }
"""

class FileInput(LayoutDOM):
    __implementation__ = IMPL
    value = String()

input = FileInput()

def upload():
    print(input.value)
button = Button(label="Upload")
button.on_click(upload)

curdoc().add_root(column(input, button))

This results in the output below:

There are almost certainly improvements that could be made to this. SO is not really a good place for iterative and collaborative discussion, so if you have questions about improving this, I'd suggest the public mailing list as the best place to continue.

这篇关于将文件信息从html文件选择器输入传递给python和bokeh的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆