1

I'm starting AWS Lambda and I fell in love with Chalice. From what I understand it has the same idea of Flask, but using all requests "serverless". I would like to put together a complex structure, with several lambda script files. I find no such example.

In an update note here, it says that from version 0.4 was added what seems to be exactly what I'm looking for. And in this example it shows just how to pull data from files inside the "chalicelib" folder.

Following this logic could create several folders within the "chalicelib", as if each folder represented a module of my application and within the folders each file would be a route with a stretch of source-code?


I wonder if this is a viable solution:

app/
├── requirements.txt
├── app.py
└── chalicelib
    ├── __init__.py
    ├── users
    │   ├── __init__.py
    │   ├── route.py
    │   └── controller.py
    └── teams
        ├── __init__.py
        ├── parameters.py
        └── controller.py

app.py:

import chalicelib

chalicelib > __init__.py:

from . import users, teams

chalicelib > users > __init__.py:

from . import route

chalicelib > users > route.py:

from app import app
from . import controller

@app.route("/users/test")
def test():
    return controller.test()
luisdemarchi
  • 1,402
  • 19
  • 29
  • Yes, you need to remember to import routes to app.py and it should work – swigganicks Jun 25 '18 at 20:04
  • @swigganicks Can you check my update? This code runs successfully, but I have doubts that when arriving at a 6 modules with about 30 routes will not be confused or overloaded the cost calculation of lambda. – luisdemarchi Jul 02 '18 at 13:27

3 Answers3

5

Chalice has a feature called blueprints. The Chalice blueprints are conceptually similar to Blueprints in Flask. Blueprints are used to organize your application into logical components. Using a blueprint, you define your resources and decorators in modules outside of your app.py. You then register a blueprint in your main app.py file. Blueprints support any decorator available on an application object. Using blueprints you can define your routes in multiple files.

Ajay K. Jain
  • 51
  • 1
  • 2
0

From Chalice 1.12.0 you need to call imports in app.py like this:

from chalice import Chalice
from chalicelib.users.controller import createUser

app = Chalice(app_name='MyApp')

@app.route('/users', content_types=['application/json'], methods=['POST'], api_key_required=True, cors=True)
def userPost():
    data = app.current_request.json_body
    response = createUser(data)
    return response

In your other file in chalicelib/users/controller.py:

def createUser(userData):
    ...{logic here}...
    return response
griff4594
  • 484
  • 3
  • 15
  • https://chalice.readthedocs.io/en/latest/topics/blueprints.html blueprint is bettersolution for logical modulation – Thirumal May 07 '21 at 11:31
  • Blueprints is where chalice stores experimental features. Read the docs and it will tell you that blueprints can do that, but it is not supported widely for that use. I've talked to the lead Architect of chalice and to import additional files you add the folder 'chalicelib' and everything inside of that folder is read on the compile of the project. That folder is where you put the additional files. – griff4594 May 08 '21 at 15:29
-4

You need to import variables into each init file until you reach the desired file. For example:

app/
|__ app.py
|__ config.py
|__ app
   |__ mod_cad
      |__ controllers.py
      |__ models.py
   |__ mod_home
      |__ controllers.py
   |__ static
      |__ ...
   |__ templates
      |__ ...
   |__ __init__.py

Then:

__init__.py:

def index():
    return render_template("home.html")

@app.errorhandler(404)
def not_found(error):
    return render_template('404.html'), 404

from app.mod_cad.controllers import mod_cad as cad_module
from app.mod_home.controllers import mod_home as home_module

app.register_blueprint(cad_module)
app.register_blueprint(home_module)

db.create_all()

And:

app.py:

# Run a test server.
from app import app
app.run(host='localhost', port=8080, debug=True)