fastapi-mvc-framework


Namefastapi-mvc-framework JSON
Version 1.2.4 PyPI version JSON
download
home_pagehttps://github.com/smjkzsl/fastapi_framework
SummarySimple and elegant use of FastApi in MVC mode
upload_time2023-04-22 11:28:13
maintainer
docs_urlNone
authorBruce chou
requires_python>=3.6
licenseApache License 2.0
keywords web framework mvc framework fastapi framework
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # fastapi_framework
A mvc framework used FastApi
Simple and elegant use of FastApi in MVC mode

[Demo](https://framework.2rails.cn/) 

### deploy one click on vercel:
[![Deploy with Vercel](https://vercel.com/button)](https://vercel.com/new/clone?repository-url=https%3A%2F%2Fgithub.com%2Fsmjkzsl%2Ffastapi-mvc-framework&project-name=fastapi-mvc-framework&repository-name=fastapi-mvc-framework)

## usage:
install:
```sh
pip install fastapi-mvc-framework
```

controller:
```python

from fastapi_mvc_framework import api_router,api,Request,Response,BaseController,application,WebSocket,WebSocketDisconnect,UploadFile,File
from typing import Dict ,List
 

application._public_auth_url = '/user/login'
application._user_auth_url = '/user/login'

@api_router(auth='public')
class TestController(BaseController): 
    @api.get("/user/login" )
    def login(self):
        """:title Login"""  
        redirect = self.get_param('redirect') if self.get_param('redirect') else '/' 
        return self.view() 
    @api.post("/test/verity_user",auth="none")
    async def verity_user(self):  
        username = self['username']
        password = self['password']
        redirect = self['redirect']
        if username and password:
            #do veritied
            if username in ['bruce','alice'] and password:
                return self._verity_successed(username,redirect)
            else:
                return self._verity_error() 
        return self._verity_error()
    
    @api.get("/user/logout")
    def logout(self):
        return self._user_logout()
    
    @api.post("/test/upload")
    async def upload_test(self,files:List[UploadFile]=File(...)):
        p = {}
        for file in files:
            path,url = await self.getUploadFile(file)
            p[file.filename] = [path,url]
        return {"files":p} 
    
    
    @api.get("/",auth='none' )
    def home(self,request:Request): 
        '''
        :title Home
        '''
        c = self.session.get('home',1)
        c = c+1  
        self.cookies["a"] = c
        if c>10:
            del self.cookies["a"]
            c = 0
        self.session['home'] = c
        text = "Hello World! I'm in FastapiMvcFramework"
        routers_map = application.routers_map
        routers = application.routes 
        return self.view()
    
    @api.get("/xml",auth='user')
    def get_legacy_data(self):
        """:title XML(only bruce)"""

        data = """<?xml version="1.0"?>
        <shampoo>
        <Header>
            Apply shampoo here.
        </Header>
        <Body>
            You'll have to use soap here.
        </Body>
        </shampoo>
        """
        return self.view(content=data,media_type="application/xml")
          
    @api.get("/chatgpt",auth="user")
    def chatgpt(self):
        """
        :title Chat(only alice)
        """
        return self.view()


 
 
websockets:Dict[str,WebSocket] = {}

@api_router(path="/{controller}")
class WSController(BaseController):  
    def __init__(self) -> None:
        super().__init__()
        
    @api.get("/" )
    def ws_home(self):
        """:title WebSocketDemo"""
        return self.view()

    @api.websocket("/chat/{client_id}")
    async def websocket_endpoint(self, websocket: WebSocket,client_id: int):
        await websocket.accept()
        websockets[client_id]=(websocket)
        try:
            while True:
                data = await websocket.receive_text()
                await websocket.send_text(f"You wrote: {data}" )
                for clientid in websockets:
                    if client_id!=clientid:
                        await websockets[clientid].send_text(f"Client #{client_id} says: {data}")
                 
        except WebSocketDisconnect:
            websockets.remove(websocket)
            for connection in websockets:
                await connection.send_text(f"Client #{client_id} left the chat")
             
```

home.html:

```html
<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <title>FastApi MVC Framework</title>
    <link rel="stylesheet" href="home.css" />

</head>

<body>
    <header style="text-align:left;display: flex;">
        <h1>Python</h1>
        <h4>on FastApi</h4>

    </header>
    <nav>
        {% for item in routers_map %} {% if 'GET' in routers_map[item]['methods'] %} {% if routers_map[item]['auth']=='none' or request.session['user'] %}
        <a href="{{routers_map[item]['path']}}">{{routers_map[item]['doc']
                and routers_map[item]['doc']['title'] or item}}</a> {% endif %} {% endif %} {% endfor %}

        <a href="#">About</a>
        <a href="#">Contact</a> {% if request.session['user'] %}
        <a href="/user/logout"><b>{{request.session['user']['username']}}</b>
                Logout</a> {% endif %}
    </nav>
    <section>
        <h2>Welcome to my website</h2>
        <p>This is an example of a responsive design that works well on both desktop and mobile devices.</p>
        <p>here is the `text` variable in class method:{{text}}</p>
        <p style="color:red"><b>{{flash}}</b></p>
    </section>
    <footer>
        <p>&copy; 2023 My Website</p>
    </footer>
</body>

</html>
```

your project directory structrue like this:
```dir

\APP
|   __init__.py
|
+---controllers
|   |   test_controller.py
|   |   __init__.py
|   |
+---models
|   |   models.py
|   |   __init__.py
|   |
+---services
|   |   user_service.py
|   |   __init__.py
|   |
+---tests
|   |   test_test.py
|   |
+---views
|   +---abc
|   |   \---2.0
|   |           css.css
|   |           home.html
|   |
|   +---test
|   |       chatgpt.css
|   |       chatgpt.html
|   |       chatgpt.js
|   |       home.html
|   |       home.js
|   |       login.html
|   |
|   \---ws
|           ws_home.html
|


```

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/smjkzsl/fastapi_framework",
    "name": "fastapi-mvc-framework",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3.6",
    "maintainer_email": "",
    "keywords": "web framework,mvc framework,fastapi framework",
    "author": "Bruce chou",
    "author_email": "smjkzsl@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/0a/16/a6b3e6173f6fcabcd3597ed4877a2042ed73bd8527d7775ae661e9e4aa1e/fastapi_mvc_framework-1.2.4.tar.gz",
    "platform": null,
    "description": "# fastapi_framework\r\nA mvc framework used FastApi\r\nSimple and elegant use of FastApi in MVC mode\r\n\r\n[Demo](https://framework.2rails.cn/) \r\n\r\n### deploy one click on vercel:\r\n[![Deploy with Vercel](https://vercel.com/button)](https://vercel.com/new/clone?repository-url=https%3A%2F%2Fgithub.com%2Fsmjkzsl%2Ffastapi-mvc-framework&project-name=fastapi-mvc-framework&repository-name=fastapi-mvc-framework)\r\n\r\n## usage:\r\ninstall:\r\n```sh\r\npip install fastapi-mvc-framework\r\n```\r\n\r\ncontroller:\r\n```python\r\n\r\nfrom fastapi_mvc_framework import api_router,api,Request,Response,BaseController,application,WebSocket,WebSocketDisconnect,UploadFile,File\r\nfrom typing import Dict ,List\r\n \r\n\r\napplication._public_auth_url = '/user/login'\r\napplication._user_auth_url = '/user/login'\r\n\r\n@api_router(auth='public')\r\nclass TestController(BaseController): \r\n    @api.get(\"/user/login\" )\r\n    def login(self):\r\n        \"\"\":title Login\"\"\"  \r\n        redirect = self.get_param('redirect') if self.get_param('redirect') else '/' \r\n        return self.view() \r\n    @api.post(\"/test/verity_user\",auth=\"none\")\r\n    async def verity_user(self):  \r\n        username = self['username']\r\n        password = self['password']\r\n        redirect = self['redirect']\r\n        if username and password:\r\n            #do veritied\r\n            if username in ['bruce','alice'] and password:\r\n                return self._verity_successed(username,redirect)\r\n            else:\r\n                return self._verity_error() \r\n        return self._verity_error()\r\n    \r\n    @api.get(\"/user/logout\")\r\n    def logout(self):\r\n        return self._user_logout()\r\n    \r\n    @api.post(\"/test/upload\")\r\n    async def upload_test(self,files:List[UploadFile]=File(...)):\r\n        p = {}\r\n        for file in files:\r\n            path,url = await self.getUploadFile(file)\r\n            p[file.filename] = [path,url]\r\n        return {\"files\":p} \r\n    \r\n    \r\n    @api.get(\"/\",auth='none' )\r\n    def home(self,request:Request): \r\n        '''\r\n        :title Home\r\n        '''\r\n        c = self.session.get('home',1)\r\n        c = c+1  \r\n        self.cookies[\"a\"] = c\r\n        if c>10:\r\n            del self.cookies[\"a\"]\r\n            c = 0\r\n        self.session['home'] = c\r\n        text = \"Hello World! I'm in FastapiMvcFramework\"\r\n        routers_map = application.routers_map\r\n        routers = application.routes \r\n        return self.view()\r\n    \r\n    @api.get(\"/xml\",auth='user')\r\n    def get_legacy_data(self):\r\n        \"\"\":title XML(only bruce)\"\"\"\r\n\r\n        data = \"\"\"<?xml version=\"1.0\"?>\r\n        <shampoo>\r\n        <Header>\r\n            Apply shampoo here.\r\n        </Header>\r\n        <Body>\r\n            You'll have to use soap here.\r\n        </Body>\r\n        </shampoo>\r\n        \"\"\"\r\n        return self.view(content=data,media_type=\"application/xml\")\r\n          \r\n    @api.get(\"/chatgpt\",auth=\"user\")\r\n    def chatgpt(self):\r\n        \"\"\"\r\n        :title Chat(only alice)\r\n        \"\"\"\r\n        return self.view()\r\n\r\n\r\n \r\n \r\nwebsockets:Dict[str,WebSocket] = {}\r\n\r\n@api_router(path=\"/{controller}\")\r\nclass WSController(BaseController):  \r\n    def __init__(self) -> None:\r\n        super().__init__()\r\n        \r\n    @api.get(\"/\" )\r\n    def ws_home(self):\r\n        \"\"\":title WebSocketDemo\"\"\"\r\n        return self.view()\r\n\r\n    @api.websocket(\"/chat/{client_id}\")\r\n    async def websocket_endpoint(self, websocket: WebSocket,client_id: int):\r\n        await websocket.accept()\r\n        websockets[client_id]=(websocket)\r\n        try:\r\n            while True:\r\n                data = await websocket.receive_text()\r\n                await websocket.send_text(f\"You wrote: {data}\" )\r\n                for clientid in websockets:\r\n                    if client_id!=clientid:\r\n                        await websockets[clientid].send_text(f\"Client #{client_id} says: {data}\")\r\n                 \r\n        except WebSocketDisconnect:\r\n            websockets.remove(websocket)\r\n            for connection in websockets:\r\n                await connection.send_text(f\"Client #{client_id} left the chat\")\r\n             \r\n```\r\n\r\nhome.html:\r\n\r\n```html\r\n<!DOCTYPE html>\r\n<html>\r\n\r\n<head>\r\n    <meta charset=\"utf-8\">\r\n    <title>FastApi MVC Framework</title>\r\n    <link rel=\"stylesheet\" href=\"home.css\" />\r\n\r\n</head>\r\n\r\n<body>\r\n    <header style=\"text-align:left;display: flex;\">\r\n        <h1>Python</h1>\r\n        <h4>on FastApi</h4>\r\n\r\n    </header>\r\n    <nav>\r\n        {% for item in routers_map %} {% if 'GET' in routers_map[item]['methods'] %} {% if routers_map[item]['auth']=='none' or request.session['user'] %}\r\n        <a href=\"{{routers_map[item]['path']}}\">{{routers_map[item]['doc']\r\n                and routers_map[item]['doc']['title'] or item}}</a> {% endif %} {% endif %} {% endfor %}\r\n\r\n        <a href=\"#\">About</a>\r\n        <a href=\"#\">Contact</a> {% if request.session['user'] %}\r\n        <a href=\"/user/logout\"><b>{{request.session['user']['username']}}</b>\r\n                Logout</a> {% endif %}\r\n    </nav>\r\n    <section>\r\n        <h2>Welcome to my website</h2>\r\n        <p>This is an example of a responsive design that works well on both desktop and mobile devices.</p>\r\n        <p>here is the `text` variable in class method:{{text}}</p>\r\n        <p style=\"color:red\"><b>{{flash}}</b></p>\r\n    </section>\r\n    <footer>\r\n        <p>&copy; 2023 My Website</p>\r\n    </footer>\r\n</body>\r\n\r\n</html>\r\n```\r\n\r\nyour project directory structrue like this:\r\n```dir\r\n\r\n\\APP\r\n|   __init__.py\r\n|\r\n+---controllers\r\n|   |   test_controller.py\r\n|   |   __init__.py\r\n|   |\r\n+---models\r\n|   |   models.py\r\n|   |   __init__.py\r\n|   |\r\n+---services\r\n|   |   user_service.py\r\n|   |   __init__.py\r\n|   |\r\n+---tests\r\n|   |   test_test.py\r\n|   |\r\n+---views\r\n|   +---abc\r\n|   |   \\---2.0\r\n|   |           css.css\r\n|   |           home.html\r\n|   |\r\n|   +---test\r\n|   |       chatgpt.css\r\n|   |       chatgpt.html\r\n|   |       chatgpt.js\r\n|   |       home.html\r\n|   |       home.js\r\n|   |       login.html\r\n|   |\r\n|   \\---ws\r\n|           ws_home.html\r\n|\r\n\r\n\r\n```\r\n",
    "bugtrack_url": null,
    "license": "Apache License 2.0",
    "summary": "Simple and elegant use of FastApi in MVC mode",
    "version": "1.2.4",
    "split_keywords": [
        "web framework",
        "mvc framework",
        "fastapi framework"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "77eefd0213a000ee1849d5b8caf982de4607f2bbbc84556b5a0c14bbb8299e51",
                "md5": "4587fe340e4bca7b42c5f5e33210848c",
                "sha256": "d76d0e84e78496920e597708a528c9fbe1cd5d5f6cf7f1fc965a167a8de05f81"
            },
            "downloads": -1,
            "filename": "fastapi_mvc_framework-1.2.4-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "4587fe340e4bca7b42c5f5e33210848c",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.6",
            "size": 32074,
            "upload_time": "2023-04-22T11:28:11",
            "upload_time_iso_8601": "2023-04-22T11:28:11.207139Z",
            "url": "https://files.pythonhosted.org/packages/77/ee/fd0213a000ee1849d5b8caf982de4607f2bbbc84556b5a0c14bbb8299e51/fastapi_mvc_framework-1.2.4-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "0a16a6b3e6173f6fcabcd3597ed4877a2042ed73bd8527d7775ae661e9e4aa1e",
                "md5": "af29d2e9509b949b944826d257e592a4",
                "sha256": "2924eca274e818b4fe210e5f9cdefc45fbc4b251cec1890c38281e42b61b89f5"
            },
            "downloads": -1,
            "filename": "fastapi_mvc_framework-1.2.4.tar.gz",
            "has_sig": false,
            "md5_digest": "af29d2e9509b949b944826d257e592a4",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.6",
            "size": 29978,
            "upload_time": "2023-04-22T11:28:13",
            "upload_time_iso_8601": "2023-04-22T11:28:13.108507Z",
            "url": "https://files.pythonhosted.org/packages/0a/16/a6b3e6173f6fcabcd3597ed4877a2042ed73bd8527d7775ae661e9e4aa1e/fastapi_mvc_framework-1.2.4.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-04-22 11:28:13",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "github_user": "smjkzsl",
    "github_project": "fastapi_framework",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": false,
    "requirements": [],
    "lcname": "fastapi-mvc-framework"
}
        
Elapsed time: 1.68149s