Quickly create web applications based on Python.
pip install cloudoll
- Operating System: Supports macOS, Linux, Windows
- Runtime Environment: Minimum requirement 3.6.0.
Now you can use cloudoll create myapp to create a new project, 3 steps to start:
- create a new project
$ cloudoll create myapp- cd to project directory
$ cd myapp- start dev server
cloudoll start -n myappYou can also manually create a project step by step.
$ mkdir cloudoll-demo && cd cloudoll-demo
$ pip3 install cloudoll
$ vi app.pyapp.py content:
## /app.py
from cloudoll.web import app
if __name__ == "__main__":
app.create().run()$ mkdir -p controllers/home
$ touch controllers/home/__init__.py
$ vi controllers/home/index.pycontrollers/home/index.py content:
# /controllers/home/index.py
from cloudoll.web import get
@get('/')
async def home():
return {"name": "cloudoll" ,"msg": "ok"}run:
$ python3 app.py
$ open http://localhost:9001Open in browser http://127.0.0.1:9001/
you can see the result:
{
"name": "cloudoll" ,
"msg": "ok" ,
"timestamp": 1681993906410
}Congratulations, you have successfully written one Restful API with cloudoll.
In most cases, we need to read the data, render the template, and then present it to the user. Therefore, we need to introduce the corresponding template engine.
in this demo,we using Nunjucks to render template.
$ mkdir templates
$ vi templates/index.htmlindex.html contents:
<!-- /templates/index.html -->
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Home</title>
</head>
<body>
<p>Hello {{name}}</p>
</body>
</html>edit /controllers/home/index.py contents:
# /controllers/home/index.py
from cloudoll.web import get, render_view
@get('/')
async def home():
data = {"name": "cloudoll" ,"msg": "ok"}
return render_view("index.html",data)at this time ,we can see “Hello cloudoll.
ok , we wrotten a view page.
We want to embed static resources such as images, JS, and CSS in the template, which requires the use of static resources. We place these js, css, and image files in the static directory.
For online environments, it is recommended to deploy to a CDN or use servers like nginx.
$ mkdir -p static/img
$ mkdir -p static/js
$ mkdir -p static/cssin img directory ,we can put images resources.
make a new js file index.js ,contents:
clike the body , we can see the alert tip "hello world"
// /static/js/index.js
document.addEventListener('DOMContentLoaded',function(){
document.body.addEventListener('click',function(){
alert('hello cloudoll.')
})
})make a new css file index.css ,contents:
/* /static/css/index.css */
html,
body {
width: 100%;
height: 100%;
color: rgb(0, 229, 255);
margin: 0;
padding: 0;
}edit the view page /templates/index.html ,in head we import the css file, like this:
<!-- /templates/index.html -->
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="icon" type="image/png" href="/static/img/logo.png"/>
<link rel="stylesheet" href="/static/css/index.css">
<script src="/static/js/index.js"></script>
<title>Home</title>
</head>
<body>
<p>My name is {{name}}</p>
</body>
</html>we create a config file, and config static resource.
$ mkdir config
$ vi config/conf.local.yaml/config/conf.local.yaml contents:
server:
static:
prefix: /staticafter reload the page , our changes will be reflected.
Suppose there is a requirement: our news site prohibits access by Baidu crawlers.
so we need to write a middleware to check User-Agent. like this:
$ mkdir middlewares
$ vi middlewares/robot.pyedit middlewares/robot.py, contents:
# /middlewares/robot.py
from cloudoll.web import middleware, render_json
import re
@middleware()
def mid_robot():
async def robot(request, handler):
ua = request.headers.get('user-agent')
if re.match('Baiduspider', ua):
return render_json(status=403, text="Go away , robot.")
return await handler(request)
return robotafter restart the server, you can use curl http://localhost:9001/news -A "Baiduspider" to see the effect.
we can see more information in Middleware
When writing business logic, it is inevitable to have configuration files. Managing configurations through code involves adding configurations for multiple environments within the code, and passing the parameter of the current environment during startup.
cloudoll support loading configurations based on the environment, defining configuration files for multiple environments
config
|- conf.local.yaml
|- conf.prod.yaml
`- conf.test.yamlnow, we create a config file:
$ mkdir -p config/conf.local.yaml
$ vi config/conf.local.yamlthe flollowing is mysql and server's configuration:
server:
host: 192.168.0.1
port: 9001
static: false
client_max_size: 1024000
static:
prefix: /static
show_index: true
append_version: true
follow_symlinks: true
database:
mysql:
host: 127.0.0.1
port: 3306
user: root
password: abcd
db: blog
charset: utf8mb4the local will be used as default configuration, when you start your application, it will load the local configuration. like cloudoll start -n myapp -env local will load the conf.local.yaml configuration.
export model from database
cloudoll gen -t users More parameters:
- -p (--path) the path to save the model
- -c (--create) to create model or create tables, default is create model
- -t (--table) The model or table name to be generated, separated by
,,ALLfor all tables - -env (--environment) to load the configuration file , default is
local - -db (--database) Database instance name, depends on the configuration file, if there are multiple databases
- -h (--help) help
cloudoll start --name myapp -env local -m developmentcloudoll start --name myapp -env prod --mode productionyou can use clodoll stop myapp to stop your application ,
or use cloudoll restart myapp to restart your application.
cloudoll list to see all your applications.