如何使用digwebs框架写下你的第一个服务器端应用
原文链接来自这里
digwebs是一个用Pyhon实现的web框架,它的源码托管在这里,感兴趣的读者可以到那里去获取代码。在这篇文章中,我将使用digwebs来实现一个Web应用,这个应用只是简单的响应浏览器的请求,并生成html页面,最终将这个html页面返回给浏览器。在实现这个应用之前,你需要安装Python 3.6.x以及获取digwebs的源码,此外你还需要根据digwebs的说明来安装jinja2
。
这篇文章的内容主要分成以下3部分:
- 运行digwebs:通过几句指令安装和运行digwebs。
- 为主页添加一个链接:为这个链接编写
blogs.html
页面,该页面以列表的方式展示文章摘要。 - digwebs的使用指南:学习基本且常用的digwebs指令,讲解这些指令在编写网页服务的作用。
这篇文章中完整的源码可以到这里获取。
运行digwebs
当你根据digwebs的readme.md文档操作之后,你将生成一个文件digwebs.py
,这个文件的内容如下:
#!/usr/bin/env python
__author__ = 'SLZ'
'''
digwebs framework demo.
'''
import logging
logging.basicConfig(level=logging.INFO)
from digwebs.web import get_app
import os
dir_path = os.path.dirname(os.path.realpath(__file__))
digwebs_app = get_app({'root_path':dir_path})
digwebs_app.init_all()
if __name__ == '__main__':
digwebs_app.run(9999, host='0.0.0.0')
else:
wsgi_app = digwebs_app.get_wsgi_application()
当你在命令行里运行指令python .\digwebs.py
,然后打开浏览器访问http://localhost:9999
,此时你将看到下图:
接下来让我们分析上面的代码是如何做到的:
from digwebs.web import get_app
这句指令从digwebs中导入了一个全局函数get_app
。digwebs_app = get_app({'root_path':dir_path})
这句指令获得一个digwebs的实例,这个实例的作用主要是将digwebs的各个模块集成在一起形成一个web框架系统。digwebs_app.run(9999, host='0.0.0.0')
这句指令主要是运行这个实例,使得这个实例能够接收外部的调用。
通过上面的3句指令,你就可以快速的启动一个完整的web服务。由于还没有为这个服务添加一些业务逻辑,因此这个服务只能简单的显示html页面。接下来我们将在文件夹views
、controllers
中添加一些html页面和python脚本来丰富我们的Web服务。
为主页添加一个链接
digwebs依赖于jinja2
来渲染html页面,因此你要确保计算机上已经安装了jinja2
。这一节的主要任务是在主页上添加一个链接,这个链接将指向另外一个页面blogs.html
,这个页面的主要作用是以列表的形式显示博客文章。为了完成以上2个任务,你需要从controllers目录中找到并编辑这个文件main_controller.py
。以下片段是该文件的内容:
#!/usr/bin/env python
__author__ = 'SLZ'
'''
digwebs framework controller.
'''
from digwebs.web import current_app
@current_app.get('/')
def hello_world():
return """
<html>
<style>
html,body{
height:100%;
padding:0;
margin:0;
}
*{
box-sizing:border-box;
}
.container{
width:100%;
height:100%;
display:flex;
justify-content:center;
align-items:center;
}
</style>
<body>
<div class="container">
<h1>digwebs - A Minimal Web Framework!</h1>
</div>
</body></html>
"""
-
在主页上添加一个链接
将<a href="/views/blogs">Read more...</a>
添加到<h1>digwebs - A Minimal Web Framework!</h1>
之后,这个链接用来跳转到blogs.html页面。保存main_controller.py
文件,并且重新运行digwebs,重新访问http://localhost:9999/
,你将看到如下界面:
-
编写blogs.html页面
当用户点击Read more ...
的时候,浏览器将跳转到blogs.html界面,在blogs页面中将以列表的方式显示文字的标题,描述,以及详细内容的链接。因此我们接下来将要编写一个blogs.html文件,并在main_controller.py
中添加一个外部能访问blogs.html的接口。
首先在文件main_controller.py
中添加接口:
@current_app.view('blogs.html')
@current_app.get('/views/blogs')
def list_blogs():
blogs = []
blogs.append({
'title':'What is digwebs',
'description':'A tiny web framework called digwebs which is developed by Python.',
'detail_link':'######'})
blogs.append({
'title':'Why you should use digwebs',
'description':'Digwebs is a Python web framework, which you can use to accelerate the development process of building a web service.',
'detail_link':'######'})
blogs.append({'title':'How to use digolds web framework','description':'You can use digwebs in a few steps. First pull the source code. Second install jinja2. Finally run python .\digwebs\project_generator.py to generate the project file structure.','detail_link':'######'})
return dict(template_blogs=blogs)
以上代码定义了接口list_blogs
,在这个接口的上面有2句指令:@current_app.view('blogs.html')
和@current_app.get('/views/blogs')
,这三句指令使得blogs.html能够被浏览器访问。在这个接口中返回了一个字典类型的数据,这个数据包含了3篇文章的数据,这三篇文章的数据将在blogs.html中呈现。接下来我们需要在views
目录中添加文件blogs.html
。
blogs.html
的内容如下:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>digwebs</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="keywords" content="python web framework, digwebs web framework,open source web framework,write web service by digwebs"/>
<meta name="description" content="A tiny web framework called digwebs which is developed by Python."/>
</head>
<body>
<div id="main" class='uk-container uk-container-small uk-padding'>
<div class="uk-child-width-1-1 uk-grid-small uk-grid-match" uk-grid>
{% for a in template_blogs %}
<div>
<div class="uk-card uk-card-default uk-card-body">
<h3 class="uk-card-title">{{{{a.title}}}}</h3>
<p>{{{{a.description}}}}</p>
<a href="{{{{a.detail_link}}}}" class="uk-text-uppercase">Read articles ...</a>
</div>
</div>
{% endfor %}
</div>
</div>
{% set static_file_prefix = 'https://cdn.jsdelivr.net/gh/digolds/digresources/' %}
<link rel="stylesheet" href="{{{{static_file_prefix}}}}/css/uikit.min.css">
<script src="{{{{static_file_prefix}}}}/js/jquery.min.js"></script>
<script src="{{{{static_file_prefix}}}}/js/uikit.min.js"></script>
<script src="{{{{static_file_prefix}}}}/js/uikit-icons.min.js"></script>
</body>
</html>
当你添加完blogs.html
之后,保存该文件。到浏览器中点击Read more ...
,主页将跳转到blogs.html
,这时,你将看到以下界面:
digwebs的使用指南
由上面的例子得知digwebs能够快速的帮助我们实现一个网站服务。实现一个网站服务一般需要以下3个步骤:
- 编写html页面,这些页面用于呈现数据
- 编写服务端的py脚本,这些脚本用于生成或获取数据,并提供给步骤1的界面消化
- 重复步骤1、2
接下来我们来分析digwebs是如何快速的帮助我们实现步骤1、2。
上面的例子中,我们定义了2个界面,主页和blogs页面。主页的内容是直接写在main_controller.py
这个文件里的,而blogs页面的内容是保存在文件blogs.html
。为了让浏览器能够访问主页,那么需要在main_controller.py
中编写以下指令来完成。
@current_app.get('/')
def hello_world():
return """
<html>
<style>
html,body{
height:100%;
padding:0;
margin:0;
}
*{
box-sizing:border-box;
}
.container{
width:100%;
height:100%;
display:flex;
justify-content:center;
align-items:center;
}
</style>
<body>
<div class="container">
<h1>digwebs - A Minimal Web Framework!</h1>
<a href="/views/blogs">Read more...</a>
</div>
</body></html>
"""
以上代码做了以下2件事情:
- 定义了一个Python函数
hello_world
,这个函数直接返回了主页内容。 - 使用指令
@current_app.get('/')
关联到函数hello_world
。这句指令的作用是告诉浏览器,要想访问主页,那么就到hello_world
中获取内容。而这句指令正是由digwebs提供的。
主页内容中有一句指令<a href="/views/blogs">Read more...</a>
,这句指令的作用在于:当用户点击Read more ...
时,浏览器将跳转到blogs.html
界面。因此类似主页的逻辑,我们也要实现以下2件事情:
- 定义一个Python函数
list_blogs
,这个函数返回3篇文章的摘要信息,包括title,description等。代码片段如下:
@current_app.view('blogs.html')
@current_app.get('/views/blogs')
def list_blogs():
blogs = []
blogs.append({
'title':'What is digwebs',
'description':'A tiny web framework called digwebs which is developed by Python.',
'detail_link':'######'})
blogs.append({
'title':'Why you should use digwebs',
'description':'Digwebs is a Python web framework, which you can use to accelerate the development process of building a web service.',
'detail_link':'######'})
blogs.append({'title':'How to use digolds web framework','description':'You can use digwebs in a few steps. First pull the source code. Second install jinja2. Finally run python .\digwebs\project_generator.py to generate the project file structure.','detail_link':'######'})
return dict(template_blogs=blogs)
- 使用指令
@current_app.get('/views/blogs')
和@current_app.view('blogs.html')
关联到函数list_blogs
。这2句话的作用是告诉浏览器,要想获得文章列表的内容,那么就到list_blogs
中获取内容,而这些内容的生成过程是:1.到@current_app.view('blogs.html')
指定的页面blogs.html
获取其中的内容;2.消化来自return dict(template_blogs=blogs)
的数据。以下代码片段来自blogs.html
,这些片段指示如何消化来自return dict(template_blogs=blogs)
的数据。
<div class="uk-child-width-1-1 uk-grid-small uk-grid-match" uk-grid>
{% for a in template_blogs %}
<div>
<div class="uk-card uk-card-default uk-card-body">
<h3 class="uk-card-title">{{{{a.title}}}}</h3>
<p>{{{{a.description}}}}</p>
<a href="{{{{a.detail_link}}}}" class="uk-text-uppercase">Read articles ...</a>
</div>
</div>
{% endfor %}
</div>
通过上面的分析你就看到,digwebs主要是将浏览器与你的业务逻辑代码连接起来形成一个整体。当你在浏览器中输入地址localhost:9999/views/blogs
,按下回车,由于指令@current_app.get('/views/blogs')
的作用,digwebs会选择在list_blogs
生成内容,内容的生成材料是由页面blogs.html
(通过指令@current_app.view('blogs.html')
指定)和return dict(template_blogs=blogs)
的数据来共同组成的。
以上就是使用digwebs的快速指南,读者可以在这个基础上添加详细文章的页面。通过这种方式来实践以及学习digwebs。