• 一个纯Python构建的Web应用框架


    4fcfa617b6d043e1947e97cbc94742df.png

    迷途小书童

    读完需要

    5

    分钟

    速读仅需 2 分钟

    1

       

    简介

    虽然现在有很多 Python web 应用框架,但大多数都是为小型数据应用程序设计的,或者使用了未经大规模验证的范式。随着应用程序复杂性的增加,代码组织、可重用性和状态往往会受到影响,结果要么是代码混乱,要么就直接使用 React 来开发应用程序了。

    Solara 解决了这一问题。使用类似 React 的应用程序接口,我们无需担心可扩展性。React 已经证明了它有能力支持世界上最大的网络应用程序。Solara 使用 React 的纯 Python 实现(Reacton),创建基于 ipywidget 的应用程序。这些应用程序既可以在 Jupyter Notebook 中运行,也可以通过 FastAPI 等框架作为独立的网络应用程序运行。这种范式实现了基于组件的代码和极其简单的状态管理。

    通过在 ipywidgets 基础上进行构建,我们可以自动利用现有的 widgets 生态系统,并在许多平台上运行,包括 JupyterLab、Jupyter Notebook、Voilà、Google Colab、DataBricks、JetBrains Datalore 等。

    2

       

    安装

    使用 pip 命令安装

    pip install solara

    3

       

    基础语法

    3.1

       

    创建响应式变量

    使用 solara.reactive() 可以创建响应式变量,它可以绑定到 UI 组件上实现数据的响应式更新

    count = solara.reactive(0)

    3.2

       

    定义组件

    使用 @solara.component 装饰器可以定义组件

    1. @solara.component
    2. def MyComponent():
    3. # 组件的实现

    组件可以显示响应式变量,也可以包含其他 solara 组件

    3.3

       

    引用组件

    直接调用组件函数即可渲染组件

    MyComponent()

    3.4

       

    更新响应式变量

    如果响应式变量更新,那么其绑定的组件也会自动更新

    count.value += 1

    solara 通过响应式变量连接数据和组件,使用简单的 Python 语法可以构建动态页面

    4

       

    示例代码

    下面是官方的一个完整代码示例

    1. import solara
    2. sentence = solara.reactive("Solara makes our team more productive.")
    3. word_limit = solara.reactive(10)
    4. @solara.component
    5. def Page():
    6. word_count = len(sentence.value.split())
    7. solara.SliderInt("Word limit", value=word_limit, min=2, max=20)
    8. solara.InputText(label="Your sentence", value=sentence, continuous_update=True)
    9. if word_count >= int(word_limit.value):
    10. solara.Error(f"With {word_count} words, you passed the word limit of {word_limit.value}.")
    11. elif word_count >= int(0.8 * word_limit.value):
    12. solara.Warning(f"With {word_count} words, you are close to the word limit of {word_limit.value}.")
    13. else:
    14. solara.Success("Great short writing!")
    15. Page()

    执行上述代码,命令是

    solara run sol.py

    会启动一个 web 服务,随即自动使用默认浏览器打开 http://localhost:8765

    9d3f1343a9cdc4e0a2fd7717e38289c8.jpeg

    这段代码使用了 solara 库实现了一个简单页面:

    导入 solara 库,创建一个响应式的句子变量 sentence 和单词限制变量 word_limit。然后,使用 solara.component 装饰器定义一个 Page 组件。在组件内部,统计 sentence 中的单词数量 word_count。使用 solara.SliderInt 创建一个滑块组件,绑定到 word_limit 变量。使用 solara.InputText 创建一个文本输入组件,绑定到 sentence 变量。根据 word_count 和 word_limit 的关系,显示不同的打印信息,如果超过限制,显示 Error 提示;如果接近限制,显示 Warning 提示,否则显示 Success 消息。最后调用 Page 组件进行渲染。这样我们就可以通过拖动滑块实时修改限制,并输入句子查看字数提示的变化。

    5

       

    参考资料

    • https://github.com/widgetti/solara

    6

       

    免费社群

    94f1729d4399f8fd508996f6f7d5bfbe.jpeg

    c03886e418ee0eafb8489eec5b7a3574.gif

  • 相关阅读:
    MySQL笔记之一致性视图与MVCC实现
    vscode使用远程服务器jupyter
    The Seven Tools of Causal Inference with Reflections on Machine Learning 文章解读
    java+springboot基于微信小程序的在线办公系统 uniapp 小程序
    docker容器内调用宿主机docker执行
    Java垃圾收集机制
    Java项目:JSP药店药品商城管理系统
    c++编写天天酷跑游戏
    Python+unittest+requests接口自动化测试框架搭建 完整的框架搭建过程
    [第十三篇]——Docker Compose
  • 原文地址:https://blog.csdn.net/djstavaV/article/details/133054347