跳转到主要内容

用Python控制网页

项目描述

Binder

纯纯

用Python控制网页 :snake

安装

安装稳定版本

pip install purly

安装开发版本

请确保首先安装npm

# clone the repository
git clone https://github.com/rmorshea/purly
cd purly && bash scripts/build.sh && bash scripts/install.sh

入门

运行以下代码片段,然后导航到http://127.0.0.1:8000/model/index

import purly

# Prepare your layout
purly.state.Machine().run(debug=False)
layout = purly.Layout('ws://127.0.0.1:8000/model/stream')

# create your HTML
div = layout.html('div')
div.style.update(height='20px', width='20px', background_color='coral')

# add it to the layout
layout.children.append(div)

# and sync it!
layout.sync()

现在您创建的内容应该神奇地出现在您打开的浏览器页面上!

div with some styling

架构(非最终版)

纯纯的基本目标是在尽可能多的页面上给予Python控制权,并在一个极简单的包中实现这一点。实现这一目标的主要问题是数据同步。纯纯对这一问题的解决方案是其模型服务器,它作为连接到它并遵守其协议的任何客户端的“事实来源”,关于网页状态

模型服务器

protocol

纯纯使用WebSocket服务器来保持多个并发客户端的同步。上面的动画显示了2个客户端——一个Python客户端向单个浏览器推送更新——然而,您可以有更多的客户端产生和/或消费模型更新。每个客户端都关联到一个单独的模型(任何可序列化为JSON的对象),但是服务器上可以存储多个模型。客户端通过指定其名称在套接字路由中连接到特定的模型(例如,ws://host:port/model/<model-name>/stream)。只有连接到相同模型的客户端才通过服务器互相通信。

模型规格

虽然模型服务器支持任何可序列化为JSON的模型,但作为控制网络的框架,Purly必须

  1. 尽可能完全地传达DOM元素的结构及其各种交互。
  2. 通过短而易于解释的包在网络上发送DOM模型更新
  • 更新消息的大小必须小,以减少网络流量。

为了实现上述目标,我们提出了一个平面DOM模型

Model = {
  id: Element,
  # Maps a uniquely identifiable string to an Element.
  root: Element,
  # The id "root" should always indicate the outermost Element.
  ...
}
Element = {
  tagName: string
  # Standard HTML tags like h1, table, div, etc.
  signature: string
  # The hash of this element attributes, and the hashes of its children.
  children: [
    string,
    # Any arbitrary string.
    {type: 'ref', 'ref': string},
    # An object where the key "ref" refers to the "key" attribute.
    ...
  ],
  attributes: {
    key: id,
    # The id that uniquely identifies this Element.
    parent_key: id
    # The unique id of this element's parent.
    attr: value,
    # Map any attribute name to any JSON serializable value.
    on<Event>: {
      # Specify an event callback with an attribute of the form "on<Event>".
      callback: uuid,
      # A unique identifier by which to refer to the callback function.
      keys: [...],
      # Details of the event to pass on to the callback.
      update: [...]
      # Any attributes that should be synced before the callback is triggered.
    }
  }
}

Purly模型示例

以下HTML

<div key='root'>
  Make a selection:
  <input type='text' key='abc123'></input>
<div>

将与以下Purly模型通信

{
  root: {
    tag: 'div',
    elements: [
      'Make a selection:'
      {'ref': 'abc123'},
    ]
    attributes: {
      'key': 'root'
    }
  },
  abc123: {
    tag: 'input',
    elements: [],
    attributes: {
      'key': 'abc123',
      'type': 'text',
    },
  }
}

通信协议

Purly模型服务器发送和接收包含以消息形式的对象的JSON可序列化数组。

[
  Message,
  # a dict conforming to the Message spec
  ...
]

消息

有两种类型的信息 - 更新和信号 - 然而,它们都符合以下格式。

Message = {
  "header": {
    "type": "signal" or "update",
    # Message type (indicates the kind of content).
    "version": "0.1",
    # Message protocol version.
  }
  # Content which depends on the message type.
  "content": dict,
}

信号

  • 信号不修改模型服务器的状态。
  • 信号的内容未修改地分发到其他模型客户端。

更新

  • 内容字段指定将合并到模型中的更改。
  • 只有更新内容与模型之间的差异才会分发给其他客户端。
  • 更新合并是按嵌套方式执行的

如果模型的当前状态是

{
  'a': {
    'b': 1,
    'c': 1,
  }
}

并且收到一个更新消息

{
  'a': {
    'c': 2
  }
}

则结果模型状态是

{
  'a': {
    'b': 1,
    'c': 2,
  }
}

项目详情


下载文件

下载适合您平台文件的文件。如果您不确定选择哪个,请了解有关安装包的更多信息。

源分发

purly-0.2.0.tar.gz (2.2 MB 查看哈希值)

上传时间

构建分发

purly-0.2.0-py3-none-any.whl (2.2 MB 查看哈希值)

上传时间 Python 3

由以下支持

AWS AWS 云计算和安全赞助商 Datadog Datadog 监控 Fastly Fastly CDN Google Google 下载分析 Microsoft Microsoft PSF 赞助商 Pingdom Pingdom 监控 Sentry Sentry 错误记录 StatusPage StatusPage 状态页面