Flask

聊聊 Python 代码覆盖率工具

寵の児 提交于 2020-07-25 10:54:56
点击上方“ AirPython ”,选择“ 加为星标 ” 第一时间关注 Python 技术干货! 1. 代码覆盖率 单元测试代码覆盖率作为一种度量方式,可以计算单元测试用例对于被测代码的覆盖程度,即:被执行的代码数量和代码总数量的比值 统计代码覆盖率 ,经常在单元测试后再进行,可以为测试结果提供评判依据 Python 项目最常使用的代码覆盖率统计工具就是: C overage 2. Coverage Coverage 是用于统计 Python 代码覆盖率的工具,不仅支持分支覆盖率统计,生成 HTML 格式的统计报告,而且可以集成到 Jenkins 中使用 安装 Coverage 依赖同样是使用 pip 安装 # 安装 Coverage 依赖 pip3 install coverage Coverage 官方提供了 2 种方式,用于统计代码覆盖率,分别是: 1、Coverage 命令行 2、Coverage API 更详细的介绍可以参考官方文档: https://coverage.readthedocs.io/en/latest/ 3. 实战一下 首先,用 Python 编写一段简单被测代码,如下: # 被测代码 # main.py def get_level (cource) : """ 自定义的方法 :param cource:成绩 :return: """ if cource

flask restful-api实现及基于flask-httpauth实现基础权限管控(三)

▼魔方 西西 提交于 2020-07-24 18:26:21
本系列教程分为四个阶段 1.flask restful web service 2.flask restful api 3.flask httpauth实现权限管控 4.uwsgi管理flask应用   前两篇文章中,我们学习了基础的web services和restful api的开发实现,在实现接口开发后,我们一般需要对接口进行加密认证,避免接口外泄导致的数据丢失等问题。   在原文中代码分为几部分说明,并不直观,而且影响连续性的学习,所以这里笔者直接通过整段代码的形式进行学习,并对代码中的关键点进行拆分讲解。   下面是实现httpauth及token认证的代码: import time from flask import Flask, jsonify, url_for, request, g from flask_restful import Api, abort # flask SQLAlchemy模块,实现ORM框架操作 from flask_sqlalchemy import SQLAlchemy # flask自带httpauth模块,BasicAuth和tokenAuth子模块 from flask_httpauth import HTTPBasicAuth, HTTPTokenAuth # 密码散列加密模块 from passlib.apps import

How to allow CORS in google cloud run?

蓝咒 提交于 2020-07-23 09:11:31
问题 I have a react microservice (via nginx) deployed onto google cloud run with its environment variable for the backend set to another google cloud run instance that's running gunicorn which is serving the backend. My Flask app is set up following everything I could find about allowing CORS: app = Flask(__name__) app.config.from_object(config) CORS(app, resources={r"/*": {"origins": "*"}}) app.config['CORS_HEADERS'] = 'Content-Type' return app # Different file, a blueprint's urls: @blueprint

Beautiful Soup, Python: Trying to display scraped contents of a for loop on an html page in the correct manner

混江龙づ霸主 提交于 2020-07-23 08:21:21
问题 Using beautiful soup and python, I have undertaken some webscraping of the shown website to isolate: the rank, company name and revenue. I would like to show, in an html table that I am rendering using flask and jinja2, the results of the top ten companies in the table, however, the code I have written is just displaying the first record five times. Code in file: webscraper.py url = 'https://en.m.wikipedia.org/wiki/List_of_largest_Internet_companies' req = requests.get(url) bsObj =

Beautiful Soup, Python: Trying to display scraped contents of a for loop on an html page in the correct manner

自作多情 提交于 2020-07-23 08:20:29
问题 Using beautiful soup and python, I have undertaken some webscraping of the shown website to isolate: the rank, company name and revenue. I would like to show, in an html table that I am rendering using flask and jinja2, the results of the top ten companies in the table, however, the code I have written is just displaying the first record five times. Code in file: webscraper.py url = 'https://en.m.wikipedia.org/wiki/List_of_largest_Internet_companies' req = requests.get(url) bsObj =

Beautiful Soup, Python: Trying to display scraped contents of a for loop on an html page in the correct manner

末鹿安然 提交于 2020-07-23 08:19:26
问题 Using beautiful soup and python, I have undertaken some webscraping of the shown website to isolate: the rank, company name and revenue. I would like to show, in an html table that I am rendering using flask and jinja2, the results of the top ten companies in the table, however, the code I have written is just displaying the first record five times. Code in file: webscraper.py url = 'https://en.m.wikipedia.org/wiki/List_of_largest_Internet_companies' req = requests.get(url) bsObj =

Tying a unique game id to a user in a Flask app to handle multiple requests to the server

三世轮回 提交于 2020-07-23 07:41:26
问题 I built a chess app with Python and used Flask to create a site for users to play on. I used Heroku to deploy the app (http://pythonchessapp.herokuapp.com/). I am new to web development and was wondering how I can handle multiple users (on separate laptops or tabs) going on the site to play the app? Something like having a unique game id per user to serve a different game to different requests. Below is some of my code for routes and initializing games. I basically initialize a Board object

Tying a unique game id to a user in a Flask app to handle multiple requests to the server

折月煮酒 提交于 2020-07-23 07:39:06
问题 I built a chess app with Python and used Flask to create a site for users to play on. I used Heroku to deploy the app (http://pythonchessapp.herokuapp.com/). I am new to web development and was wondering how I can handle multiple users (on separate laptops or tabs) going on the site to play the app? Something like having a unique game id per user to serve a different game to different requests. Below is some of my code for routes and initializing games. I basically initialize a Board object

flask-wtforms. QuerySelectField RuntimeError

本小妞迷上赌 提交于 2020-07-23 06:38:21
问题 Im trying to use QuerySelectField in FlaskForm, but getting error. admin/forms.py class ServiceForm(FlaskForm): # def __init__(self, *args, **kwargs): # super(ServiceForm, self).__init__(*args, **kwargs) # self.category.choices = [(cat.id, cat.name) for cat in ServiceCategory.query.all()] category = QuerySelectField('Category', query_factory=ServiceCategory.query, get_pk=lambda a: a.id, get_label=lambda a: a.name) name = StringField('Name', validators=[DataRequired()]) description =

flask-wtforms. QuerySelectField RuntimeError

送分小仙女□ 提交于 2020-07-23 06:37:49
问题 Im trying to use QuerySelectField in FlaskForm, but getting error. admin/forms.py class ServiceForm(FlaskForm): # def __init__(self, *args, **kwargs): # super(ServiceForm, self).__init__(*args, **kwargs) # self.category.choices = [(cat.id, cat.name) for cat in ServiceCategory.query.all()] category = QuerySelectField('Category', query_factory=ServiceCategory.query, get_pk=lambda a: a.id, get_label=lambda a: a.name) name = StringField('Name', validators=[DataRequired()]) description =