Flask 自建扩展

  • 开源发布准备

*
1. 添加文档字符串与注释后的完整代码

"""
 Flask-Share
 # ~~~~~~~~~~~~~~
 Create social share component in Jinja2 tempalte based on share.js.

 :copyright: (c) 2017 by Gavin Li.

 :license: MIT, see LICENSE for more details.

"""

import re
from flask import current_app, url_for, Markup, Blueprint, request

class Share(object):

 @staticmethod
  def load(css\_url=None, js\_url=None):
    """ Load share.js resourse.

 :param css\_url: if set, will be used as css url
 :param js\_url: if set, will be used as js url
 :param serve\_local: if set to True, the local resource will be used
"""

 @staticmethod
  def create( title='', sites=None, mobile\_sites=None,align='left',addtion\_class=''):
    """ Create a share component.

 :param title: the prompt displayed on the left of the share component.

 :param sites: a string that consist of sites, separate by comma.

 :param mobile\_sites: a string that consist of sites, separate by comma.

 supported site name: weibo, wechat, douban, facebook, twitter, google, linkedin, qq, qzone."
 for example: weibo,wechat, qq.

 :param mobile\_sites: the sites displayed on mobile.

 :param align: the align of the share component,default to 'left'.

 :param addition\_class: the style class added to the share component.

"""

*
1. 编写 README 与文档
– 小项目 直接用 README概括所有的必需的说明
– 大项目 比较复杂的,多文件组织文档内容
将项目部署到Read the Docs
Sphinx + Github + Readthedocs的工作流编写和部署文档
*
1. 定义 python 包的元数据:(setup.py)

"""
 Flask-Share

 Create social share component in Jinja2 template based on share.js.

 :copyright: (c) 2022 by Gavin li.

 :license: MIT, see LICENSE for more details.

"""
form os import path
from codecs import open
form setuptools import setup

basedir = path.abspath(path.dirname(__file__))

# Get the long description from the README file
with open(path.join(basedir,'README.md'), encoding='utf-8') as f:
  long_description = f.read()

setup(
  name='Flask-Share', # 包名称
  version='0.1.0',  # 版本
  url='https://github.com/lghpython/flask-share',
  license='MIT',
  author='xxx'
  author_email='xx@xx.com',
  description='xxx',
  long_description=long_description,
  long_description_content_type='text/markdown', # 默认渲染格式为 rst
  platforms='any',
  packages=['flask\_share'], # 包含的包列表,包括子包,可用find\_pakages()
  zip_safe=False,
  test_suite='test\_flask\_share', 测试包或模块
  include_package_data=True,
  install_requires=['Flask'],  # 安装依赖
  keywords='flask extension development', # 项目关键词
  classifiers=[ # 分类词, 在 PyPI 中设置分类
    'DevelopmentStatus::3-Alpha',
    'Environment::WebEnvironment',
    'IntendedAudience::Developers',
    'License::OSIApproved::MITLicense',
    'ProgrammingLanguage::Python',
    'ProgrammingLanguage::Python::2',
    'ProgrammingLanguage::Python::2.7',
    'ProgrammingLanguage::Python::3',
    'ProgrammingLanguage::Python::3.3',
    'ProgrammingLanguage::Python::3.4',
    'ProgrammingLanguage::Python::3.5',
    'ProgrammingLanguage::Python::3.6',
    'Topic::Internet::WWW/HTTP::DynamicContent',
    'Topic::SoftwareDevelopment::Libraries::PythonModules']
  ],
)

*
1. 指定打包其他文件: MANIFEST.in
需要在 setup()方法中设置: include_package_data=True

graft flask_share/static
include LICENSE test_flask_share.py
# exclude 用来排除匹配文件
# recursive-include 递归匹配
# recursive-exclude 递归排除匹配
# graft 目录 包含目录下所有
# prune 目录 配出目录下所有

*
1. 编写单元测试

import unittest

from flask import Flask, render_template_string, current_app
from flask_share import Share

class ShareTestCase(unittest.TestCase):

  def setUp(self):
    self.mobile_agent={{'HTTP\_USER\_AGENT':'Mozilla/5.0(iPhone;CPUiPhoneOS9\_1likeMacOSX)\
 AppleWebKit/601.1.46(KHTML,likeGecko)Version/9.0Mobile/13B143Safari/601.1'}}
    app = Flask(__name__)
    app.testing=True
    self.share=Share(app)

 @app.route('/')
    def index():
      return render_template_string('{{share.load() }}\n {{share.create() }}')
    # 推送上下文
    self.context=app.app_context()
    self.context.push()
    self.client - app.test_client()

  def tearDown(self):
    self.context.pop()

  def test\_create\_on\_mobile(self):
    current_app.config['SHARE\_HIDE\_ON\_MOBILE'] = True
    response = self.client.get('/', environ_base=self.mobile_agent)
    data = response.get_data(as_text=True)
    self.assertIn('social-share.min.js', data)
    self.assertNotIn(', data))

*
1. setup.cfg

Original: https://blog.csdn.net/weixin_45566993/article/details/123343724
Author: 李自提
Title: Flask 自建扩展



相关阅读

Title: anaconda安装-超详细版

文章目录

一、anaconda安装

Anaconda 是一个用于科学计算的 Python 发行版,支持 Linux, Mac, Windows, 包含了众多流行的科学计算、数据分析的 Python 包。装anaconda,就不需要单独装python了。
anaconda优点:

  • 1、anaconda里面集成了很多关于python科学计算的第三方库,主要是安装方便,而python是一个编译器,如果不使用anaconda,那么安装起来会比较痛苦,各个库之间的依赖性就很难连接的很好。
  • 2、常见的科学计算类的库都包含在里面了,使得安装比常规python安装要容易。

1.1 anaconda安装地址

anaconda安装包下载,不建议去官网下载,官网下载太慢了,推荐下载地址 点击这里到清华镜像站下载

Flask 自建扩展
一般建议不要选择最新版的(不太稳定),我这建议选择2019-2020中的anaconda3随意一个版本(我用的是2019-10-16的Windows-x86_64.exe,这个anaconda自带的python是3.7版本的)

; 1.2 安装详细步骤

安装过程如下:

Flask 自建扩展

在这里选择”All Users”

Flask 自建扩展
安装路径:提醒小白,如果c盘允许,安装到C盘可以避免一些小问题,但是我电脑内存小,所以装入了E盘。

Flask 自建扩展

最容易错的部分来了。
第一项Add Anaconda… 这个是说将安装路径自动添加系统环境变量,强烈建议勾选上,后续可以省去很多麻烦。(不用去手动添加环境变量)

第二项 是说要默认使用python的版本,选上!!

Flask 自建扩展

点击install,等待几分钟就安装完成了。

Flask 自建扩展
提示安装VScode,选择点击”skip”
Flask 自建扩展

对于两个”learn”,都取消打勾,不用打开去看了,耽误时间。

Flask 自建扩展

1.3 检验安装是否成功

在cmd中输入 :python(按回车键),——查看是否有Python环境

Flask 自建扩展
要先退出python(ctrl+z)后,再接着输入:conda –version,——查看是否有conda环境
Flask 自建扩展

; 1.4 更改conda源(后续安装第三方库可以加快速度)

在Anaconda prompt中操作:

conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/
conda config --set show_channel_urls yes

查看是否修改好通道:

conda config --show channels

Original: https://blog.csdn.net/in546/article/details/117400839
Author: plasma-deeplearning
Title: anaconda安装-超详细版

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/300910/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载