def test_url_prefix(): app = Luya('test_url_prefix') bp = Blueprint('test_url_prefix', prefix_url='/bp') @bp.route('/') def handler(request): return text('Hello') app.register_blueprint(bp) rsp = app.test_client.get('/bp/') assert rsp.text == 'Hello'
def test_with_bp_with_url_prefix(): app = Luya('test_with_bp_with_url_prefix') bp = Blueprint('test_text', prefix_url='/test1') class DummyView(MethodView): def get(self, request): return text('I am get method') bp.add_route(DummyView.to_view(), '/') app.register_blueprint(bp) response = app.test_client.get('/test1/') assert response.text == 'I am get method'
def test_with_bp(): app = Luya('test_with_bp') bp = Blueprint('test_text') class DummyView(MethodView): def get(self, request): assert request.stream is None return text('I am get method') bp.add_route(DummyView.to_view(), '/') app.register_blueprint(bp) response = app.test_client.get('/') assert app.has_stream is False assert response.text == 'I am get method'
def test_basic_blueprint(): ''' To test basic blueprint. ''' app = Luya('test_basic_blueprint') bp = Blueprint('test_basic_blueprint') @bp.route('/') def handler(request): return text('Hello') app.register_blueprint(bp) rsp = app.test_client.get('/') assert app.has_stream == False assert rsp.text == 'Hello'
def test_url_with_multipe_bp_prefix(): app = Luya('test_url_prefix') bp = Blueprint('test_url_prefix', prefix_url='/bp1') bp2 = Blueprint('test_url_prefix', prefix_url='/bp2') @bp.route('/') def handler(request): return text('Hello1') @bp2.route('/') def handler(request): return text('Hello2') app.register_blueprint(bp) app.register_blueprint(bp2) rsp1 = app.test_client.get('/bp1/') assert rsp1.text == 'Hello1' rsp2 = app.test_client.get('/bp2/') assert rsp2.text == 'Hello2'
#!/usr/bin/env python3 import aiohttp from luya import Luya from luya import response from luya import blueprint from foodcal import food_bp app = Luya() if __name__ == '__main__': app.register_blueprint(food_bp) app.run()
if '脂肪' in str(item.text): count += 1 self.food_specs['脂肪'] = result[index + 1].text if count >= 3: break @app.route('/<foodname>/<gram:number>') async def helloWorld(request, foodname=None, gram=100): food = SearchFood(foodname, gram=gram) await food.search() food_specs = food.specs rsp_html = ''' <div> <p>名字:{}</p> <p>热量:{}</p> <p>碳水化合物:{}</p> <p>脂肪:{}</p> <p>蛋白质:{}</p> </div>'''.format(food_specs['real_name'], food_specs['cal'], food_specs['碳水化合物'], food_specs['脂肪'], food_specs['蛋白质']) return response.html(rsp_html) if __name__ == '__main__': app.register_blueprint(bp) app.run()