The following basic setup allows you to run the app locally in a Docker container.
-
Install the following tools:
- docker
- docker-compose (comes pre-installed on Mac OS)
- pip
- pipenv
- An IDE (we like PyCharm)
-
Create a
.env
file in the project root:SENTRY_DSN=<local Sentry DSN>
-
Run
docker-compose build && docker-compose up
to start the Docker container. It's expected that a lot of messages will be printed. When it is ready and you visit http://localhost:8000, it should look similar to below. You will see:- a panel (left) where you can enter a query. See README.md for examples.
- a panel (center) where results of queries will appear when you write a query and click the "play" button on the top bar.
- a panel (right) for clicking around to explore our GraphQL query model.
-
This is an API on the GraphQL protocol. When you visit localhost:8000, you will have:
Run pipenv install <package>
to add a package to Pipfile and Pipfile.lock,
and to install it to the environment. Add --dev
if the package is not needed in production.
At the time of writing, SciPy, pip, and BigSur aren't compatible. This will likely be solved by an update in pip and/or SciPy in the near future. Until then:
- Ask someone on Linux (Mathijs) or macOS Catalina (Chelsea) to add dependencies to Pipfile.
- Spin up a Python docker container:
docker-compose -f docker-compose-python-macosbigsur.yml run python /bin/bash
- In the Docker container, run the following:
pip install pipenv
cd recsapi
pipenv install <package>
orpipenv update <package>
for all necessary packages
The steps below allow you to run the app locally, without any risk of impacting AWS resources. DynamoDB is simulated using LocalStack. Unfortunately, currently does not support Feature Groups which RecommendationAPI uses to get engagement metrics for the Firefox New Tab.
- Complete the 'Basic setup' above.
- Follow the steps to
configure an interpreter using Docker-Compose
on Confluence.
- Set 'Service' to 'app_dev' when adding the interpreter.
- Choose the 'main' run configuration in PyCharm from the dropdown menu in the top-right.
- To test your setup, set a breakpoint in app/main.py on
app = FastAPI()
, and visit http://localhost:8000. - Try running a query from README.md.
The steps bellow allow you to run the app locally, while using AWS resources such as DynamoDB and Feature Groups in Pocket-Dev. Data is not guaranteed to exist consistently in this environment, so you might have to insert any required records first.
- Follow the steps under 'Debugging the app locally in PyCharm' first to ensure you are able to run the app locally.
- Follow the steps to
configure an interpreter using Docker-Compose
on Confluence.
- Set 'Configuration files' to
./docker-compose-dev.yml
. - Set 'Service' to 'app_dev'.
- Set 'Configuration files' to
- Choose the 'main Pocket-Dev' run configuration in PyCharm from the dropdown menu in the top-right.
- Generate/insert data in Pocket-Dev.
- Run
source <(maws -o awscli)
and choose thepocket-dev-PocketSSODataLearning
profile.
Follow steps 1-2 from the above section 'Debugging the app in PyCharm', to add a Docker-Compose interpreter.
With the remote interpreter selected as the project default, there are many ways in PyCharm to run tests:
- Right click on the test directory and choose 'Run pytest in tests'.
- Click the play icon next to individual tests or test classes.
- Add a pytest run configuration: (See screenshot below.)
- Click Run > Edit configurations.
- Add a pytest configuration by clicking the + icon and choose Python tests > pytest.
- Set 'Module name' to "tests".
- Set the 'Python interpreter' to the Remote interpreter.
- Set the working directory to the project root.
Create a Pipenv virtual environment:
cd aws_lambda
pipenv install --dev
Repeat the above steps any time git pulls in changes to aws_lambda/Pipfile.lock.
PyCharm can only add Pipenv files located in the project root. Use the following work-around to add a Pipenv interpreter using the Pipfile located in aws_lambda/.
- Choose File > Open, and open recommandation-api/aws_lambda in a new PyCharm project.
- Open PyCharm > Preferences > Project: aws_lambda > Python Interpreter.
- Click the cog in the top-right, and choose 'add interpreter'.
- Choose 'PipEnv', and click 'OK'.
- Close the aws_lambda PyCharm project.
Now use the aws_lambda Pipenv interpreter in the recommendation-api PyCharm project:
- Open PyCharm > Preferences > Project > Python Interpreter.
- Click the cog in the top-right, and choose 'Show all'.
- Select "Pipenv (aws_lambda)", to make this the project default.
- Wait until PyCharm finishes indexing files.
- Add a pytest run configuration: (See screenshot below.)
- Click Run > Edit configurations.
- Add a pytest configuration by clicking the + icon and choose Python tests > pytest.
- Set 'Module name' to "aws_lambda.tests".
- Set the 'Python interpreter' to the Pipenv (aws_lambda) interpreter.
- Set the working directory to
<project root path>/aws_lambda
.
The tests pass reliably in CircleCI, but we currently don't have a local Docker environment to run them in, which makes them less reliably locally. We've seen the following error locally:
botocore.exceptions.NoRegionError: You must specify a region.
If you see this, don't hesitate to ask for help in #team-backend. We could set up a simple Python docker container to isolate the tests from the host environment, which is how we run them in .circleci/config.yml.