Python的Web框架之CherryPy的介绍、安装、基本使用

Python的Web框架之CherryPy的介绍、安装、基本使用1 CherryPy:=========1.1 是一种用于Python的、简单而非常有用的Web框架。1.2 可以运行在最新版本的Python、

大家好,欢迎来到IT知识分享网。

1 CherryPy:

=========

1.1 是一种用于Python的、简单而非常有用的Web框架。

1.2 可以运行在最新版本的Python、Jython、Android上。

1.3 其主要作用是以尽可能少的操作将Web服务器与Python代码连接。

1.4 是 CGI 的优秀替代品,并且是创建 Python Web 应用程序的良好基础。

Python的Web框架之CherryPy的介绍、安装、基本使用

2 环境:

=====

华为笔记本电脑,深度deepin-linux操作系统,python3.8,微软vscode编辑器,谷歌浏览器。

3 官网:

=====

安装
pip install CherryPy
#本机安装
#sudo pip3.8 install CherryPy   #有点慢,放弃
sudo pip3.8 install -i https://mirrors.aliyun.com/pypi/simple CherryPy #超快

4 安装:

=====

  WARNING: The script calc-prorate is installed in '/usr/local/python3.8/bin' which is not on PATH.
  Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.
  WARNING: The script cheroot is installed in '/usr/local/python3.8/bin' which is not on PATH.
  Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.
  WARNING: The script cherryd is installed in '/usr/local/python3.8/bin' which is not on PATH.
  Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.

提醒:没有设置路径,暂时不管,估计没有设置软连接。

  WARNING: The script calc-prorate is installed in '/usr/local/python3.8/bin' which is not on PATH.
  Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.
  WARNING: The script cheroot is installed in '/usr/local/python3.8/bin' which is not on PATH.
  Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.
  WARNING: The script cherryd is installed in '/usr/local/python3.8/bin' which is not on PATH.
  Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.

5 hello world:

5.1 方法一,代码:

import cherrypy
class HelloWorld(object):
    @cherrypy.expose
    def index(self):
        return "Hello World!"
cherrypy.quickstart(HelloWorld())

运行示范:

Python的Web框架之CherryPy的介绍、安装、基本使用

5.2 方法二,代码:

import cherrypy  
class HelloWorld:  
    def index(self):  
        return "Hello world!"  
    index.exposed = True   #暴露设置为true,区别
cherrypy.quickstart(HelloWorld())  

5.3 方法三,代码:

import cherrypy
class HelloWorld(object):
    @cherrypy.expose
    def index(self):
        return "Hello world!"
if __name__ == '__main__':
    cherrypy.quickstart(HelloWorld())

6 显示html的plotly的柱状图法

import cherrypy
class HtmlShow(object):
    @cherrypy.expose
    def index(self):
        return """<html>
	<head>
		<meta charset="utf-8" />
		<title>Javascript与Plotly结合的可视化作图</title>
	</head>
	<body>
		<div id="myDiv" style="width:600px;height:550px;"></div>
	</body>
	<script src="https://cdn.plot.ly/plotly-latest.min.js"></script>
	<!--以下需要修改数据-->
	<script>
		// 饼图的数据源
		var data = [{
			x: ['apple', 'orange', 'banana'],
			y: [20, 11, 25],
			type: 'bar'
		}];
		Plotly.newPlot('myDiv', data);
	</script>
    <!--以上需要修改数据-->
</html>"""

if __name__ == '__main__':
    cherrypy.quickstart(HtmlShow())

Python的Web框架之CherryPy的介绍、安装、基本使用

===整理并分享===

喜欢就点赞、转发和收藏。

自己可以继续深入学习。

免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://yundeesoft.com/58349.html

(0)
上一篇 2024-05-05 13:00
下一篇 2024-05-08 09:26

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注

关注微信