Skip to main content

python gen amis json options

Project description

pyamis

目的: 在python中生成amisjson配置
解决问题:

  1. 手写json配置太累了,配置复制粘贴重用性不好,减少手写json拼写出错概率
  2. 可以在python中通过代码快速生成json配置
  3. 要能和python的dict混用

install

git clone https://github.com/1178615156/pyamis
pip install --editable .

示例

生成Form组件

# 首先import下
from pyamis.components import *

form = Form(
    target='target',
    mode="horizontal",
    # 可以看到属性能够直接插入dict
    controls=[
        {
            "type": "submit",
            "label": "登录"
        }
    ]
)

这样就生成了个Form配置了,然后转成json看看

import json

json.dumps(form)

转成json

{
  "type": "form",
  "mode": "horizontal",
  "controls": [
    {
      "type": "submit",
      "label": "\u767b\u5f55"
    }
  ],
  "target": "target"
}

和dict进一步结合示例

form_default_options = dict(
    title='条件',
    mode='horizontal',
    horizontal={
        "leftFixed": "sm"
    },
    submitOnInit=False,
    autoFocus=False,
)
form = Form(**form_default_options, #可以直接插入dict
            target='target')

# 直接设置属性也可以
form['controls'] = [
    {
        "type": "submit",
        "label": "提交"
    }
]

目前有的组件有:

快速启动示例

from flask import Flask, send_file
from pyamis.components import *
import json

app = Flask("pyamis-test")


# app-pages配置
def site():
    return [
        # 第一个page
        AppPage(
            label='page-1',
            url='1',
            # 第一个page有2个子页
            children=[
                # 子页1
                AppPage(
                    label="page-1-1",
                    url='page-1-1',
                    schema_body=[
                        Form(controls=[
                            {
                                'type': 'text',
                                'name': "email",
                                'label': "email",
                                "required": True
                            },
                        ]),
                        "hello world 1 - 1"
                    ]
                ),
                # 子页2
                AppPage(
                    label="page-1-2",
                    url='page-1-2',
                    # schema_body=[...] 等价于
                    schema=Page(body=[
                        'hello page 1 - 2 '
                    ]),
                ),
            ]
        ),
        # 第二个page
        AppPage(
            label='page-2',
            url='2',
            #如果想用的组件没有的话,可以直接写json
            schema_body=[
                {
                    "type": "service",
                    "api": "https://3xsw4ap8wah59.cfc-execute.bj.baidubce.com/api/amis-mock/mock2/page/initData",
                    "body": {
                        "type": "panel",
                        "title": "$title",
                        "body": "现在是:${date}"
                    }
                }
            ],
        )
    ]

# print下生成的json配置
print(json.dumps(site(),indent=2))

@app.route('/')
def root():
    return send_file('test_index.html')


# 分页的json
@app.route("/pages/site.json")
def site_json():
    return {
        'data': {
            'pages': [
                dict(label='Home', url='/'),
                dict(children=site())
            ]
        }
    }


if __name__ == '__main__':
    app.run(port=8080, debug=True)

Project details


Download files

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

Source Distribution

amispy-0.0.2.tar.gz (20.2 kB view details)

Uploaded Source

Built Distribution

amispy-0.0.2-py3-none-any.whl (29.5 kB view details)

Uploaded Python 3

File details

Details for the file amispy-0.0.2.tar.gz.

File metadata

  • Download URL: amispy-0.0.2.tar.gz
  • Upload date:
  • Size: 20.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.5.0.1 requests/2.22.0 requests-toolbelt/0.9.1 tqdm/4.36.1 CPython/3.7.4

File hashes

Hashes for amispy-0.0.2.tar.gz
Algorithm Hash digest
SHA256 afcb7176e3978f2a3f3042a1880529ca1d273dd8dcad74c887e19d28a7f5a0fc
MD5 ecb9b73ed1d538575bbbd51d784c82db
BLAKE2b-256 fdb946c9b295fa511ed3b50462a445a3ef2a641f5483f71fe25ece7ec06993c1

See more details on using hashes here.

File details

Details for the file amispy-0.0.2-py3-none-any.whl.

File metadata

  • Download URL: amispy-0.0.2-py3-none-any.whl
  • Upload date:
  • Size: 29.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.5.0.1 requests/2.22.0 requests-toolbelt/0.9.1 tqdm/4.36.1 CPython/3.7.4

File hashes

Hashes for amispy-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 6bec324d113aaa64fde38dde9712a5fa770a43541290668d1f9fddc9c9a45e57
MD5 7363e3f033b70a389034919ec3b2273a
BLAKE2b-256 11dc6ac8dbeceea3a9bf6bc11a4e97e601f8f23876744751e2bdeade6d0d0173

See more details on using hashes here.

Supported by

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