aws / aws/chalice

Routes in multiple python files

Open
#881 2 comments 6 reactions 0 assignees View on GitHub
feature-request
Dominant language
Python
Stars
11.1k
Forks
1k
Avg merge
1d 22h
Merged PRs (30d)
2

Description

Can I create multiple files with routes? I would like to separate by "modules" and each module be a folder with the routes and all scripts. In the release note of 0.4 it is commented that the "chalicelib" folder has been added but does not speak if I can get the routes from the "app.pyf".

As I understand it, what I can do is create all the routes in the main file and make imports and each auxiliary function. But I do not think it would be a clean project when I hit 40 API calls.

A current test:
(importing the scripts only when the correct function is requested)

**app.py**
```
app.route('/recordings',
methods=['POST'])
def create_recording():
from chalicelib.polly_s3 import synthesize_speech, upload_to_s3, index_in_dynamodb;

request = app.current_request
body = request.json_body
record_id = str(uuid.uuid4())
text = body.get("text")
voice = 'Vitoria'

synthesize_speech(record_id, text, voice)
url = upload_to_s3(record_id, S3_BUCKET)
item = index_in_dynamodb(record_id, text, voice, url, DYNAMO_DB_TABLE)
return [item]
```

-------------

I wonder if this is a viable solution:

**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()
```

Contributor guide

Open the contributing guide

Research direction

Start by reviewing the issue's app.py and chalicelib import examples, including chalicelib/users/route.py and its controller import. Determine whether route definitions can be split across modules and how the proposed import structure should work. Done means the supported approach is defined and covered by an appropriate example or test.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
api, backend
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.