Skip to main content

如何发布一个Flask扩展包

如何发布一个Flask扩展包

如何发布一个Flask扩展包

如何开发和发布一个Flask扩展,如何开发一个Flask扩展,官方介绍在这里

如何开发Flask扩展 - 实战篇(以我最近开发的flask_dxcaptcha为例)

OS - MAX OS

  • 创建一个目录flask_dxcaptcha
mkdir flask_dxcaptcha
cd flask_dxcaptcha
  • 创建python虚拟环境并激活
virtualenv .env -p  /usr/local/bin/python3
source .env/bin/activate
  • 创建包并开发具体的逻辑(flask_dxcaptcha目录下)
mkdir flask_dxcaptcha
cd flask_dxcaptcha
touch __init__.py
  • 编写__init__.py文件开始开发扩展逻辑,记录下flask_dxcaptcha的__init__.py
# -*- coding: utf-8 -*-
# @Author: durban.zhang
# @Date:   2019-12-25 17:02:16
# @Last Modified by:   durban
# @Last Modified time: 2019-12-25 18:43:25

from flask import current_app, _app_ctx_stack

from flask_dxcaptcha.captchaclient import CaptchaClient


class DXCaptcha(object):

    def __init__(self, app=None):
        self.app = app
        if app is not None:
            self.init_app(app)

    def init_app(self, app):
        app.config.setdefault('DX_APP_ID', '')
        app.config.setdefault('DX_APP_SECRECT', '')

    def init_captcha_client(self):
        if current_app.config['DX_APP_ID'] == '' or \
                current_app.config['DX_APP_SECRECT'] == '':
            raise Exception('DX_APP_ID and DX_APP_SECRECT can not empty.')

        return CaptchaClient(
            current_app.config['DX_APP_ID'],
            current_app.config['DX_APP_SECRECT'],
        )

    @property
    def client(self):
        ctx = _app_ctx_stack.top
        if ctx is not None:
            if not hasattr(ctx, 'dx_captcha'):
                ctx.dx_captcha = self.init_captcha_client()
            return ctx.dx_captcha

flask_dxcaptcha包下的目录结构如下

.
├── __init__.py
├── captchaclient.py
├── captcharesponse.py
├── ctuclient.py
├── cturequest.py
├── cturesponse.py
├── cturesponsestatus.py
├── cturesult.py
├── hitrule.py
├── risklevel.py
├── risktype.py
├── suggestion.py
└── suggestpolicy.py

然后先项目目录下面添加如下几个文件 setup.py # 具体打包和安装配置 MANIFEST.in # 打包文件配置 README.md # 项目介绍包括如何安装、如何使用 LICENSE # 项目许可类型

这里比较重要的是setup.py和MANIFEST.in,着重记录下这两个文件,其余两个文件可以去参考这个扩展flask_dxcaptcha

  • setup.py的配置如下
# -*- coding: utf-8 -*-
# @Author: durban
# @Date:   2019-12-25 18:00:43
# @Last Modified by:   durban.zhang
# @Last Modified time: 2019-12-25 19:03:55

from setuptools import find_packages, setup


setup(
    name='Flask-DXCaptcha',
    version='1.0.0',
    url='https://github.com/durban89/flask_dxcaptcha',
    license='MIT',
    author='durban zhang',
    author_email='durban.zhang@gmail.com',
    description='Flask-DXCaptcha是依赖顶象科技提供的无感验证功能开发的Flask 扩展',
    long_description=__doc__,
    packages=find_packages(),
    zip_safe=False,
    include_package_data=True,
    platforms='any',
    install_requires=[
        'Flask>=1.1.1'
    ],
    python_requires=">=3.7.4",
    classifiers=[
        'Environment :: Web Environment',
        'Intended Audience :: Developers',
        'License :: OSI Approved :: BSD License',
        'Operating System :: OS Independent',
        'Programming Language :: Python :: 3',
        'Topic :: Internet :: WWW/HTTP :: Dynamic Content',
        'Topic :: Software Development :: Libraries :: Python Modules'
    ]
)

具体关于setup参数的介绍,请点这里

  • MANIFEST.in的配置如下
include MANIFEST.in
include README
include LICENSE
recursive-exclude *.pyc

具体关于参数的介绍,请点这里

如何发布Flask扩展

如何发布Flask扩展,具体的可以参考官方介绍,点这里

这里以flask_dxcaptcha为例介绍下,前提是项目开发后,自己测试没有问题再发布

在flask_dxcaptcha项目目录下面运行如下命令(注意是我们的虚拟环境

  • 如果没有安装setuptoolswheel执行下面命令
pip install setuptools wheel
  • 打包扩展
python3 setup.py sdist bdist_wheel

在dist目录下生成如下文件

$ tree dist
dist
├── Flask-DXCaptcha-1.0.0.tar.gz
└── Flask_DXCaptcha-1.0.0-py3-none-any.whl
  • 上传包到Test PyPI

注册Test PyPI账号,点这里注册,然后安装twine,执行下面命令

pip install twine -i https://mirrors.aliyun.com/pypi/simple/

-i 参数指定了包的镜像源

上传所有打包文件到Test PyPI

twine upload --repository-url https://test.pypi.org/legacy/ dist/*

示例如下

$ twine upload --repository-url https://test.pypi.org/legacy/ ./dist/*
Uploading distributions to https://test.pypi.org/legacy/
Enter your username: [your_username]
Enter your password:
Uploading Flask_DXCaptcha-1.0.0-py3-none-any.whl
100%|███████████████████████████████████████████████████████████████████████████████████████████████| 13.0k/13.0k [00:04<00:00, 3.01kB/s]
Uploading Flask-DXCaptcha-1.0.0.tar.gz
100%|███████████████████████████████████████████████████████████████████████████████████████████████| 9.53k/9.53k [00:01<00:00, 7.57kB/s]

View at:
https://test.pypi.org/project/Flask-DXCaptcha/1.0.0/

测试安装上传的包

pip install -i https://test.pypi.org/simple/ --no-deps Flask-DXCaptcha==1.0.0

如果安装成功,就可以正常使用了

$ python
Python 3.7.4 (default, Jul  9 2019, 18:14:44)
[Clang 9.0.0 (clang-900.0.39.2)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import flask_dxcaptcha
>>>

如果以上都是正常包的话,那么将包发布到正式的PyPI中

twine upload dist/*

示例如下

$ twine upload dist/*
Uploading distributions to https://upload.pypi.org/legacy/
Enter your username: durban_zhang
Enter your password:
Uploading Flask_DXCaptcha-1.0.0-py3-none-any.whl
100%|███████████████████████████████████████████████████████████████████████████████████████████████| 13.0k/13.0k [00:09<00:00, 1.41kB/s]
Uploading Flask-DXCaptcha-1.0.0.tar.gz
100%|███████████████████████████████████████████████████████████████████████████████████████████████| 9.53k/9.53k [00:01<00:00, 6.87kB/s]

View at:
https://pypi.org/project/Flask-DXCaptcha/1.0.0/

打开地址 https://pypi.org/project/Flask-DXCaptcha/1.0.0/ 就能看到发布的Flask扩展了

版权声明

版权声明

张大鹏 创作并维护的 Walkerfree 博客采用 创作共用保留署名-非商业-禁止演绎4.0国际许可证。本文首发于 Walkerfree 博客(http://www.walkerfree.com/),版权所有,侵权必究。本文永久链接:http://www.walkerfree.com/article/188