Skip to main content

Assist in quickly creating distributable microservices based on aiohttp, with a focus on simplicity and ease of use.

Project description

🔥🔥🔥 Cloudoll

Quickly create web applications based on Python.

Documentation

Docs

Structure

Config

Middleware

Router

View

Cookie and Session

Upload file

WebSocket

Mysql

Deployment

Install

pip install cloudoll

Environment

  • Operating System: Supports macOS, Linux, Windows
  • Runtime Environment: Minimum requirement 3.6.0.

Quick Start

Now you can use cloudoll create myapp to create a new project, 3 steps to start:

  1. create a new project
$ cloudoll create myapp
  1. cd to project directory
$ cd myapp
  1. start dev server
cloudoll start -n myapp

You can also manually create a project step by step.

$ mkdir cloudoll-demo && cd cloudoll-demo
$ pip3 install cloudoll
$ vi app.py

app.py content:

## /app.py
from cloudoll.web import app


if __name__ == "__main__":
    app.create().run()

Controller

$ mkdir -p controllers/home
$ touch controllers/home/__init__.py
$ vi controllers/home/index.py

controllers/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:9001

Open 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.

Template rendering

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.html

index.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.

static files

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/css

in 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: /static

after reload the page , our changes will be reflected.

Middleware

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.py

edit 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 robot

after restart the server, you can use curl http://localhost:9001/news -A "Baiduspider" to see the effect. we can see more information in Middleware

Configuration

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.yaml

now, we create a config file:

$ mkdir -p config/conf.local.yaml
$ vi config/conf.local.yaml

the 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: utf8mb4

the 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.

cli

Create model

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 ,, ALL for 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

Development and debugging

cloudoll start --name myapp -env local -m development

Production Environment

cloudoll start --name myapp -env prod --mode production

you 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.

Project details


Release history Release notifications | RSS feed

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

cloudoll-3.0.13.tar.gz (45.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

cloudoll-3.0.13-py3-none-any.whl (51.7 kB view details)

Uploaded Python 3

File details

Details for the file cloudoll-3.0.13.tar.gz.

File metadata

  • Download URL: cloudoll-3.0.13.tar.gz
  • Upload date:
  • Size: 45.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.5

File hashes

Hashes for cloudoll-3.0.13.tar.gz
Algorithm Hash digest
SHA256 be7bd8ac6b43903ae5a4c8d02168879b61fda19a00b6cfa893bd5602981a326d
MD5 18242984597e438eec15c5c8a459b1ed
BLAKE2b-256 456270ac03ce24babeda1a788f5ce05b976a5dd2110affc0f0f2ea41445b33ad

See more details on using hashes here.

File details

Details for the file cloudoll-3.0.13-py3-none-any.whl.

File metadata

  • Download URL: cloudoll-3.0.13-py3-none-any.whl
  • Upload date:
  • Size: 51.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.5

File hashes

Hashes for cloudoll-3.0.13-py3-none-any.whl
Algorithm Hash digest
SHA256 6fff1dc584d73b83a0e64350a88f65f57c78bb928e2aaa7799e46a580b716883
MD5 9e4b9ea0e56cfaffefeac16f56a787f8
BLAKE2b-256 5f265ae2bff21da3e5a3e2548ff7fcbc3a83bcc839e8b79ccd67f3f211f6182d

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page