从浏览器中创建的 table 提取用户输入到 python?

Extract user input to python from a table created in browser?

我想在使用 python 创建的浏览器中创建一个 table。这部分可以通过使用 bokeh 库的 DataTable 来完成。问题是当用户在 table 本身中输入 his/her 时,我想从 table 中提取数据。 我可以使用 python 的任何库来执行此操作吗?不过,如果我能用散景来做到这一点就更好了。

您可以使用BeautifulSoup it's great for parse HTML content, see this example

如果您的要求是使用 Python 创建应用程序,用户将通过浏览器访问并将一些数据更新到 table?

使用 Django 或任何网络框架,基本上,您正在尝试构建网络应用程序!!

如果您正在寻找其他东西,请彻底提及您的要求。

bokeh中使用的光滑网格数据table用户可以直接编辑:

http://docs.bokeh.org/en/latest/docs/reference/models/widgets.tables.html.

由于数据table的每一列的数据可以对应于 ColumnDataSource 的一个字段,因此可以创建 python 或 javascript 回调,以检测对table 中的数据值。然后,您可以访问所需用例的更新数据。

下面是编辑数据时使用 javascript 回调的示例。编辑数据时,更新的列将打印到浏览器控制台。请注意,它仅在您编辑值然后单击单元格外后检测到数据已更改。

如果您想 运行 在 python 函数之外基于用户输入,您可以使用 python 回调执行完全相同的操作。不过,这确实需要 运行 设置散景服务器才能工作。

from datetime import date
from bokeh.io import output_file, show
from bokeh.layouts import widgetbox
from bokeh.models import ColumnDataSource, CustomJS
from bokeh.models.widgets import DataTable, DateFormatter, TableColumn, StringEditor

output_file("data_table.html")

data = dict(
        dates=[date(2014, 3, i+1) for i in range(10)],
        strings=['edit_this' for i in range(10)],
    )
source = ColumnDataSource(data)

columns = [
        TableColumn(field="dates", title="Date", formatter=DateFormatter()),
        TableColumn(field="strings", title="Downloads", editor=StringEditor()),
    ]

data_table = DataTable(source=source, columns=columns, width=400, height=280,
                       editable=True)

# callback code to detect user edit of table
code = """
    data = source.data
    console.log('data has been updated!')
    console.log(data['strings'])
"""

callback = CustomJS(code=code,args={'source':source})
source.js_on_change('data', callback)

show(widgetbox(data_table))

编辑:

这是一个使用 python 回调的类似示例。当您编辑单元格时,本示例中的所有单元格都将被替换。显然你可以做任何你想做的事这只是一个例子。

您需要在源上设置回调以响应数据的变化。因此 source.on_change('data', update)。阅读更多

https://docs.bokeh.org/en/latest/docs/user_guide/interaction/widgets.html

from datetime import date
from bokeh.io import curdoc
from bokeh.layouts import widgetbox
from bokeh.models import ColumnDataSource
from bokeh.models.widgets import DataTable, DateFormatter, TableColumn, StringEditor

data = dict(
        dates=[date(2014, 3, i+1) for i in range(10)],
        strings=['edit_this' for i in range(10)],
    )
source = ColumnDataSource(data)

columns = [
        TableColumn(field="dates", title="Date", formatter=DateFormatter()),
        TableColumn(field="strings", title="Downloads", editor=StringEditor()),
    ]

data_table = DataTable(source=source, columns=columns, width=400, height=280,
                       editable=True)

# callback code to detect user edit of table
def update(attrname, old, new):
    data = source.data
    data['strings'] = ['you just edited the table.']*10
    source.data = data

source.on_change('data', update)

curdoc().add_root(widgetbox(data_table))