Brazil Election Forecast — Get the Code
The complete code from How to Set Up a Machine Learning or Data Science Project.
What’s inside #
data.py— fetch and clean a Wikipedia polling tablemodel.py— the method: average last N polls, predict the winnervalidate.py— checks the method against the 2018 and 2022 runoffsmain.py— orchestrates validation + the 2026 forecast, writesforecast.jsonpresent.py— rendersforecast.jsoninto a deployable page.github/workflows/forecast.yml— reruns the forecast on a schedule.gitlab-ci.yml— the CI setup that builds and deploys the live sitetest_*.py— the full test suite, including a live test against the deployed URLCOMMIT.txt— the exact commit this zip was built fromLICENSE— MIT, free to reuse and adapt
Running it #
Unzip the download, then from inside the folder:
uv sync
uv run main.py
This writes forecast.json and prints the forecast.
To run the tests:
uv run pytest
Requires uv and Python 3.12+.
Refer to the instructions in How to Set Up a Machine Learning or Data Science Project for how to deploy.