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.3.tar.gz (20.5 kB view details)

Uploaded Source

Built Distribution

amispy-0.0.3-py3-none-any.whl (30.3 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: amispy-0.0.3.tar.gz
  • Upload date:
  • Size: 20.5 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.3.tar.gz
Algorithm Hash digest
SHA256 85758ba4daecc7facd5c12d005a6a57f459bdef986d1fa7607ed33f7fffb43e2
MD5 33f0e4b3b61afc8294f034eee64a0961
BLAKE2b-256 43f17141c2af080821987a469453654f9c94636f849b9ce3bfaa816298cb5a9b

See more details on using hashes here.

File details

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

File metadata

  • Download URL: amispy-0.0.3-py3-none-any.whl
  • Upload date:
  • Size: 30.3 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.3-py3-none-any.whl
Algorithm Hash digest
SHA256 7d3075f1239503e8149bcdc44e8d5632167e64d32fabe7cb1fdc55642b43ebd4
MD5 affbfa59244a08aa84869d4734cd0c4c
BLAKE2b-256 8452554841e6824dda9262d22bda218383776f6458ab22290bf92cc0d7778dff

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