Procházet zdrojové kódy

feat: add `pyinstaller_datas()` and pyinstaller package usage

wangweimin před 4 roky
rodič
revize
f258daaf26

+ 31 - 0
docs/libraries_support.rst

@@ -1,6 +1,37 @@
 Libraries support
 ======================
 
+Build stand-alone App
+----------------------
+`PyInstaller <https://pyinstaller.readthedocs.io/en/stable/>`_ bundles a Python application and all its dependencies into a folder or executable. The user can run the packaged app without installing a Python interpreter or any modules.
+
+You can use PyInstaller to packages PyWebIO application into a stand-alone executable or folder:
+
+1. Create a pyinstaller spec (specification) file::
+
+    pyi-makespec <options> app.py
+
+   You need replace ``app.py`` to your PyWebIO application file name.
+
+2. Edit the spec file, change the ``datas`` parameter of ``Analysis``::
+
+    from pywebio.util import pyinstaller_datas
+
+    a = Analysis(
+        ...
+        datas=pyinstaller_datas(),
+        ...
+
+3. Build the application by passing the spec file to the pyinstaller command::
+
+    pyinstaller app.spec
+
+
+If you want to create a one-file bundled executable, you need pass ``--onefile`` option in first step.
+
+For more information, please visit: https://pyinstaller.readthedocs.io/en/stable/spec-files.html
+
+
 .. _visualization:
 
 Data visualization

+ 139 - 70
docs/locales/zh_CN/LC_MESSAGES/libraries_support.po

@@ -7,52 +7,121 @@ msgid ""
 msgstr ""
 "Project-Id-Version: PyWebIO 1.1.0\n"
 "Report-Msgid-Bugs-To: \n"
-"POT-Creation-Date: 2021-02-17 17:57+0800\n"
-"PO-Revision-Date: 2021-02-18 11:26+0800\n"
+"POT-Creation-Date: 2021-03-14 21:46+0800\n"
+"PO-Revision-Date: 2021-03-14 21:45+0800\n"
+"Last-Translator: WangWeimin <wang0.618@qq.com>\n"
+"Language: zh_CN\n"
+"Language-Team: \n"
+"Plural-Forms: nplurals=1; plural=0\n"
 "MIME-Version: 1.0\n"
 "Content-Type: text/plain; charset=utf-8\n"
 "Content-Transfer-Encoding: 8bit\n"
 "Generated-By: Babel 2.8.0\n"
-"Last-Translator: WangWeimin <wang0.618@qq.com>\n"
-"Language-Team: \n"
-"Language: zh_CN\n"
-"X-Generator: Poedit 2.4.2\n"
 
 #: ../../libraries_support.rst:2
 msgid "Libraries support"
 msgstr "第三方库生态"
 
-#: ../../libraries_support.rst:7
+#: ../../libraries_support.rst:5
+msgid "Build stand-alone App"
+msgstr "构建stand-alone App"
+
+#: ../../libraries_support.rst:6
+msgid ""
+"`PyInstaller <https://pyinstaller.readthedocs.io/en/stable/>`_ bundles a "
+"Python application and all its dependencies into a folder or executable. "
+"The user can run the packaged app without installing a Python interpreter"
+" or any modules."
+msgstr ""
+"`PyInstaller <https://pyinstaller.readthedocs.io/en/stable/>`_  "
+"用于将一个Python应用及其依赖打包到文件夹或可执行文件中,用户可以在不安装Python解释器以及任何模块的情况下运行打包后的应用程序。"
+
+#: ../../libraries_support.rst:8
+msgid ""
+"You can use PyInstaller to packages PyWebIO application into a stand-"
+"alone executable or folder:"
+msgstr "可以使用PyInstaller来将PyWebIO应用打包成一个单独的可执行文件或文件夹:"
+
+#: ../../libraries_support.rst:10
+msgid "Create a pyinstaller spec (specification) file::"
+msgstr "创建pyinstaller spec (specification) 文件::"
+
+#: ../../libraries_support.rst:12
+msgid "pyi-makespec <options> app.py"
+msgstr ""
+
+#: ../../libraries_support.rst:14
+msgid "You need replace ``app.py`` to your PyWebIO application file name."
+msgstr "你需要将 ``app.py`` 替换成你PyWebIO应用的文件名。"
+
+#: ../../libraries_support.rst:16
+msgid "Edit the spec file, change the ``datas`` parameter of ``Analysis``::"
+msgstr "编辑生成的spec文件,将其中 `` Analysis`` 的 ``data`` 参数修改为::"
+
+#: ../../libraries_support.rst:18
+msgid ""
+"from pywebio.util import pyinstaller_datas\n"
+"\n"
+"a = Analysis(\n"
+"    ...\n"
+"    datas=pyinstaller_datas(),\n"
+"    ..."
+msgstr ""
+
+#: ../../libraries_support.rst:25
+msgid ""
+"Build the application by passing the spec file to the pyinstaller "
+"command::"
+msgstr "使用spec文件来构建可执行文件::"
+
+#: ../../libraries_support.rst:27
+msgid "pyinstaller app.spec"
+msgstr ""
+
+#: ../../libraries_support.rst:30
+msgid ""
+"If you want to create a one-file bundled executable, you need pass "
+"``--onefile`` option in first step."
+msgstr "如果你希望生成一个单独的可执行文件而不是文件夹,你需要在第一步时传入 ``—onefile`` 选项。"
+
+#: ../../libraries_support.rst:32
+msgid ""
+"For more information, please visit: "
+"https://pyinstaller.readthedocs.io/en/stable/spec-files.html"
+msgstr ""
+"更多PyInstaller用法请见: https://pyinstaller.readthedocs.io/en/stable/spec-"
+"files.html"
+
+#: ../../libraries_support.rst:38
 msgid "Data visualization"
 msgstr "数据可视化"
 
-#: ../../libraries_support.rst:8
+#: ../../libraries_support.rst:39
 msgid "PyWebIO supports for data visualization with the third-party libraries."
 msgstr "PyWebIO支持使用第三方库进行数据可视化"
 
-#: ../../libraries_support.rst:11
+#: ../../libraries_support.rst:42
 msgid "Bokeh"
 msgstr ""
 
-#: ../../libraries_support.rst:13
+#: ../../libraries_support.rst:44
 msgid ""
-"`Bokeh <https://github.com/bokeh/bokeh>`_ is an interactive visualization "
-"library for modern web browsers. It provides elegant, concise construction of "
-"versatile graphics, and affords high-performance interactivity over large or "
-"streaming datasets."
-msgstr ""
-"`Bokeh <https://github.com/bokeh/bokeh>`_ 是一个支持创建实时交互的数据可视化库。"
+"`Bokeh <https://github.com/bokeh/bokeh>`_ is an interactive visualization"
+" library for modern web browsers. It provides elegant, concise "
+"construction of versatile graphics, and affords high-performance "
+"interactivity over large or streaming datasets."
+msgstr "`Bokeh <https://github.com/bokeh/bokeh>`_ 是一个支持创建实时交互的数据可视化库。"
 
-#: ../../libraries_support.rst:15
+#: ../../libraries_support.rst:46
 msgid ""
 "You can use ``bokeh.io.output_notebook(notebook_type='pywebio')`` in the "
-"PyWebIO session to setup Bokeh environment. Then you can use ``bokeh.io."
-"show()`` to output a boken chart::"
+"PyWebIO session to setup Bokeh environment. Then you can use "
+"``bokeh.io.show()`` to output a boken chart::"
 msgstr ""
-"在 PyWebIO 会话中调用 ``bokeh.io.output_notebook(notebook_type='pywebio')`` 来设"
-"置Bokeh输出到PyWebIO::"
+"在 PyWebIO 会话中调用 ``bokeh.io.output_notebook(notebook_type='pywebio')`` "
+"来设置Bokeh输出到PyWebIO::"
 
-#: ../../libraries_support.rst:18
+#: ../../libraries_support.rst:49
 msgid ""
 "from bokeh.io import output_notebook\n"
 "from bokeh.io import show\n"
@@ -63,123 +132,123 @@ msgid ""
 "show(fig)"
 msgstr ""
 
-#: ../../libraries_support.rst:26
+#: ../../libraries_support.rst:57
 msgid "See related demo on :charts_demo_host:`bokeh demo </?app=bokeh>`"
 msgstr "相应demo见 :charts_demo_host:`bokeh demo </?app=bokeh>`"
 
-#: ../../libraries_support.rst:28
+#: ../../libraries_support.rst:59
 msgid ""
 "In addition to creating ordinary charts, Bokeh can also build the Bokeh "
-"applications by starting the `Bokeh server <https://docs.bokeh.org/en/latest/"
-"docs/user_guide/server.html>`_. The purpose of the Bokeh server is to make it "
-"easy for Python users to create interactive web applications that can connect "
-"front-end UI events to real, running Python code."
+"applications by starting the `Bokeh server "
+"<https://docs.bokeh.org/en/latest/docs/user_guide/server.html>`_. The "
+"purpose of the Bokeh server is to make it easy for Python users to create"
+" interactive web applications that can connect front-end UI events to "
+"real, running Python code."
 msgstr ""
-"除了创建普通图表,Bokeh还可以通过启动Bokeh server来显示Bokeh app,Bokeh app支持"
-"向图表的添加按钮、输入框等交互组件,并向组件注册Python回调,从而创建可以与Python"
-"代码交互的图表。"
+"除了创建普通图表,Bokeh还可以通过启动Bokeh server来显示Bokeh app,Bokeh "
+"app支持向图表的添加按钮、输入框等交互组件,并向组件注册Python回调,从而创建可以与Python代码交互的图表。"
 
-#: ../../libraries_support.rst:30
+#: ../../libraries_support.rst:61
 msgid ""
-"In PyWebIO, you can also use ``bokeh.io.show()`` to display a Bokeh App. For "
-"the example, see `bokeh_app.py <https://github.com/wang0618/PyWebIO/blob/dev/"
-"demos/bokeh_app.py>`_."
+"In PyWebIO, you can also use ``bokeh.io.show()`` to display a Bokeh App. "
+"For the example, see `bokeh_app.py "
+"<https://github.com/wang0618/PyWebIO/blob/dev/demos/bokeh_app.py>`_."
 msgstr ""
-"在PyWebIO中,你也可以使用 ``bokeh.io.show()`` 来显示一个Bokeh App,代码示例见 "
-"`bokeh_app.py <https://github.com/wang0618/PyWebIO/blob/master/demos/bokeh_app."
-"py>`_。"
+"在PyWebIO中,你也可以使用 ``bokeh.io.show()`` 来显示一个Bokeh App,代码示例见 `bokeh_app.py "
+"<https://github.com/wang0618/PyWebIO/blob/master/demos/bokeh_app.py>`_。"
 
-#: ../../libraries_support.rst:32
+#: ../../libraries_support.rst:63
 msgid "Bokeh App currently is only available in the default Tornado backend"
 msgstr "Bokeh App当前仅支持默认的Tornado后端"
 
-#: ../../libraries_support.rst:37
+#: ../../libraries_support.rst:68
 msgid "pyecharts"
 msgstr ""
 
-#: ../../libraries_support.rst:39
+#: ../../libraries_support.rst:70
 msgid ""
-"`pyecharts <https://github.com/pyecharts/pyecharts>`_  is a python plotting "
-"library which uses `Echarts <https://github.com/ecomfe/echarts>`_ as underlying "
-"implementation."
+"`pyecharts <https://github.com/pyecharts/pyecharts>`_  is a python "
+"plotting library which uses `Echarts "
+"<https://github.com/ecomfe/echarts>`_ as underlying implementation."
 msgstr ""
 "`pyecharts <https://github.com/pyecharts/pyecharts>`_ 是一个使用Python创建 "
 "`Echarts <https://github.com/ecomfe/echarts>`_ 可视化图表的库。"
 
-#: ../../libraries_support.rst:41
+#: ../../libraries_support.rst:72
 msgid ""
 "In PyWebIO, you can use the following code to output the pyecharts chart "
 "instance::"
 msgstr ""
-"在 PyWebIO 中使用 `put_html() <pywebio.output.put_html>` 可以输出 pyecharts 库创"
-"建的图表::"
+"在 PyWebIO 中使用 `put_html() <pywebio.output.put_html>` 可以输出 pyecharts "
+"库创建的图表::"
 
-#: ../../libraries_support.rst:43
+#: ../../libraries_support.rst:74
 msgid ""
 "# `chart` is pyecharts chart instance\n"
 "pywebio.output.put_html(chart.render_notebook())"
 msgstr ""
 
-#: ../../libraries_support.rst:46
+#: ../../libraries_support.rst:77
 msgid "See related demo on :charts_demo_host:`pyecharts demo </?app=pyecharts>`"
 msgstr "相应demo见 :charts_demo_host:`pyecharts demo </?app=pyecharts>`"
 
-#: ../../libraries_support.rst:53
+#: ../../libraries_support.rst:84
 msgid "plotly"
 msgstr ""
 
-#: ../../libraries_support.rst:55
+#: ../../libraries_support.rst:86
 msgid ""
-"`plotly.py <https://github.com/plotly/plotly.py>`_ is an interactive, open-"
-"source, and browser-based graphing library for Python."
+"`plotly.py <https://github.com/plotly/plotly.py>`_ is an interactive, "
+"open-source, and browser-based graphing library for Python."
 msgstr ""
-"`plotly.py <https://github.com/plotly/plotly.py>`_ 是一个非常流行的Python数据可"
-"视化库,可以生成高质量的交互式图表。"
+"`plotly.py <https://github.com/plotly/plotly.py>`_ "
+"是一个非常流行的Python数据可视化库,可以生成高质量的交互式图表。"
 
-#: ../../libraries_support.rst:57
+#: ../../libraries_support.rst:88
 msgid ""
-"In PyWebIO, you can use the following code to output the plotly chart instance::"
+"In PyWebIO, you can use the following code to output the plotly chart "
+"instance::"
 msgstr "PyWebIO 支持输出使用 plotly 库创建的图表。使用方式为在PyWebIO会话中调用::"
 
-#: ../../libraries_support.rst:59
+#: ../../libraries_support.rst:90
 msgid ""
 "# `fig` is plotly chart instance\n"
 "html = fig.to_html(include_plotlyjs=\"require\", full_html=False)\n"
 "pywebio.output.put_html(html)"
 msgstr ""
 
-#: ../../libraries_support.rst:63
+#: ../../libraries_support.rst:94
 msgid "See related demo on :charts_demo_host:`plotly demo </?app=plotly>`"
 msgstr "相应demo见 :charts_demo_host:`plotly demo </?app=plotly>`"
 
-#: ../../libraries_support.rst:68
+#: ../../libraries_support.rst:99
 msgid "cutecharts.py"
 msgstr ""
 
-#: ../../libraries_support.rst:70
+#: ../../libraries_support.rst:101
 msgid ""
 "`cutecharts.py <https://github.com/cutecharts/cutecharts.py>`_ is a hand "
-"drawing style charts library for Python which uses `chart.xkcd <https://github."
-"com/timqian/chart.xkcd>`_ as underlying implementation."
+"drawing style charts library for Python which uses `chart.xkcd "
+"<https://github.com/timqian/chart.xkcd>`_ as underlying implementation."
 msgstr ""
-"`cutecharts.py <https://github.com/cutecharts/cutecharts.py>`_ 是一个可以创建具"
-"有卡通风格的可视化图表的python库。\n"
+"`cutecharts.py <https://github.com/cutecharts/cutecharts.py>`_ "
+"是一个可以创建具有卡通风格的可视化图表的python库。\n"
 "底层使用了 `chart.xkcd <https://github.com/timqian/chart.xkcd>`_ Javascript库。"
 
-#: ../../libraries_support.rst:72
+#: ../../libraries_support.rst:103
 msgid ""
-"In PyWebIO, you can use the following code to output the cutecharts.py chart "
-"instance::"
+"In PyWebIO, you can use the following code to output the cutecharts.py "
+"chart instance::"
 msgstr ""
 "在 PyWebIO 中使用 `put_html() <pywebio.output.put_html>` 可以输出 cutecharts.py "
 "库创建的图表::"
 
-#: ../../libraries_support.rst:74
+#: ../../libraries_support.rst:105
 msgid ""
 "# `chart` is cutecharts chart instance\n"
 "pywebio.output.put_html(chart.render_notebook())"
 msgstr ""
 
-#: ../../libraries_support.rst:77
+#: ../../libraries_support.rst:108
 msgid "See related demo on :charts_demo_host:`cutecharts demo </?app=cutecharts>`"
 msgstr "相应demo见 :charts_demo_host:`cutecharts demo </?app=cutecharts>`"

+ 16 - 2
pywebio/utils.py

@@ -9,11 +9,25 @@ import string
 import time
 from collections import OrderedDict
 from contextlib import closing
-from os.path import abspath, dirname
+from os.path import abspath, dirname, join, normpath
 
 project_dir = dirname(abspath(__file__))
 
-STATIC_PATH = '%s/html' % project_dir
+STATIC_PATH = join(project_dir, 'html')
+
+
+def pyinstaller_datas(cli_args=False):
+    """Return data files included in the PyWebIO to be added to pyinstaller bundle."""
+    datas = [
+        (STATIC_PATH, 'pywebio/html'),
+        (normpath(STATIC_PATH + '/../platform/tpl'), 'pywebio/platform/tpl')
+    ]
+    if cli_args:
+        args = ''
+        for item in datas:
+            args += ' --add-data %s%s%s' % (item[0], os.pathsep, item[1])
+        return args
+    return datas
 
 
 class Setter: