JeffyLu.github.io
JeffyLu.github.io copied to clipboard
bokeh文件伪上传
bokeh文件伪上传
环境依赖
- bokeh >= 0.12.4
- tornado >= 4.0(不支持最新的4.5)
- nodejs
代码
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))
注意
- 如果页面一直没响应,可能是tornado版本不兼容
- 如果出现
500: Internal Server Error,可能是nodejs的问题 - 文件实际是没有上传到服务器的,服务器只是接收了文件名
参考
Pass file information from html file selector input to python and bokeh