Create a virtual environment and install the python dependencies:
python3 -m venv .venv
source .venv/bin/activate
pip install -r requirements.in -r requirements_dev.in
For this you'll need to install node > v20.9.0.
nvm install --lts
nvm use --lts
npm install
Once installed you now have access to GOVUK components, stylesheets and JS via the node_module
To copy some of the assets you'll need into your project, run the following:
npm run build
Ensure you do this before starting your Flask project as you're JS and SCSS imports will fail in the flask run.
Create your local config file .env
from the template file:
cp .env.example .env
Don't worry, you can't commit your .env
file.
source .venv/bin/activate
flask --app app run --debug --port=8020
Now you can browse: http://127.0.0.1:8020
(The default port for flask apps is 5000, but on Macs this is often found to conflict with Apple's Airplay service, hence using another port here.)
For local development and deployments, run the below code:
./run_local.sh
To run the tests:
python -m pytest --cov=app --cov-report=term-missing --cov-branch
To run all tests:
pytest
To run unit tests:
pytest tests/unit_tests
To run functional/non-functional tests:
pytest tests/functional_tests
Run tests in headed mode:
pytest --headed
Run tests in a different browser (chromium, firefox, webkit):
pytest --browser firefox
Run tests in multiple browsers:
pytest --browser chromium --browser webkit
If you are running into issues where it states a browser needs to be installed run:
playwright install
For further guidance on writing tests https://playwright.dev/python/docs/writing-tests
The following will:
- Generate requirement.txt files from files inside requirements/source/.in and put them into requirements/generated/.txt
- Run linting checks with ruff
- Run secret detection via trufflehog3
pre-commit install
The Ruff linter looks for code quality issues. Ensure there are no ruff issues before committing.
To lint all files in the directory, run:
ruff check
To format all files in the directory, run:
ruff format
The trufflehog3 package looks for any exposed secrets in your project.
To use trufflehog on your current project, run:
trufflehog3 filesystem .
We are using the Flask-Babel package to translate text. There are 4 key components to translating text on the website.
- babel.cfg - Identifies which files to look for strings that can be translated
- ./bin/translate.sh - Script to collect/update all translatable strings
- pybabel compile - Should be run after updating any messages.po files. The full command is given in the output of translate.sh script
- There are two languages(English and Welsh) available on the site. No translation is provided for English as that is the default language
- Wrap text in
{% trans %}...{% endtrans %}
- Run
./bin/translate.sh
- Update
app/translations/cy/LC_MESSAGES/messages.po
with welsh text - Run
pybabel compile -d app/translations -l cy -f
from flask_babel import lazy_gettext as _
_("text to translate")
- Run
./bin/translate.sh
- Update
app/translations/cy/LC_MESSAGES/messages.po
with welsh text - Run `pybabel compile -d app/translations -l