Development¶
Technology¶
Atramhasis is a python webapplication that is being developed within the pyramid framework. Other major technologies used are sqlalchemy as the ORM and Jinja2 as the templating framework.
Client side there are two interfaces. The public interface is server rendered and styled with Foundation (version 5) and SCSS. The admin interface is a Vue 3 application built with Vite and styled with Tailwind CSS and Nuxt UI.
While Atramhasis is an editor for creating and editing SKOS vocabularies, it uses other libraries that are more geared towards using a vocabulary in an application.
skosprovider: This library defines a
VocabularyProvider. This is an abstraction of usefull functionalities an application integrating SKOS needs. Different libraries can implement this interface for different datasources. This allows decoupling the interface from the concrete implementation. Out of the box this comes with a simpleDictionaryProviderthat serves a vocabulary based on a simple pythondictas datasource.skosprovider_sqlalchemy: An implementation of the
VocabularyProviderinterface with a SQLAlchemy backend. This allows using a RDBMS for reading, but also writing, SKOS concepts.skosprovider_rdf: An implemenation of the
VocabularyProviderinterface with an RDF backend. Atramhasis uses this for exporting ConceptSchemes to RDF. It can also be used to get an existing SKOS vocabulary defined in RDF into Atramhasis.pyramid_skosprovider: A library that integrates pyramid and skosprovider. This libraries creates a
skosprovider.registry.Registryand makes it accessible through thepyramid.request.Request. Is also exposes a set of readonly REST services on the registered providers.skosprovider_getty: An implemenation of the
VocabularyProvideragainst the Linked Open Data vocabularies published by the Getty Research Institute at http://vocab.getty.edu such as the Art and Architecture Thesaurus (AAT) and the Thesaurus of Geographic Names (TGN).skosprovider_heritagedata: An implementation of the
VocabularyProvideragainst the vocabularies published by EH, RCAHMS and RCAHMW at heritagedata.org.
Atramhasis can easily be set up with a Linked Data Fragments (LDF) server. This server allows posing simple triple pattern queries of your dataset. Combined with a Linked Data Fragments client, similar functionalities to a traditional SPARQL endpoint can be achieved. Atramhasis facilitates the setup of a Linked Data Fragments server by generating a suitable config file for the Javascript server. This server can use different backends. Out of the box, Atramhasis generates Turtle files that can be used by the LDF server. It’s also possible to configre Atramhasis with a link to the rdf2hdt command (requires a separate installation). In this case, everytime the conceptschemes are dumped to RDF, the dump files are also written in HDT format.
Development setup with mise¶
The quickest way to get a full development environment is mise. It installs the tools (Python, uv, Node, pnpm, pre-commit), creates and activates the virtualenv, and drives the setup through a set of tasks. From a fresh clone:
$ mise trust # trust this config (depending on your mise settings)
$ mise install # install python, uv, node, pnpm and pre-commit
$ mise run server # runs the setup the first time, then starts the dev stack
mise run server starts the Pyramid dev server on http://localhost:6543 and the Vite dev server for the Vue admin app on http://localhost:5173. Press Ctrl-C once to stop both.
The first run (or any run without a virtualenv) performs mise run setup, which sets up everything described under General installation and Admin and frontend development:
the virtualenv with the dependencies from the lockfile matching the pinned Python version, and Atramhasis itself in editable mode;
the compiled gettext message catalogs;
the development database: alembic upgrade head, and for a new database the demo vocabularies (initialize_atramhasis_db) plus a first RDF datadump (dump_rdf) - see
scripts/setup_backend.sh;the pre-commit git hooks;
the frontend: pnpm install, the Vite build, and the generated
atramhasis/templates/admin.jinja2- seescripts/build_frontend.py, which is the same code the wheel build hook uses, so a local setup and a wheel build produce an identical frontend.
Once everything is set up, mise run server starts immediately. The database is never rebuilt as long as it exists, so local data is not lost. Existing databases are still migrated to head on every setup.
To rebuild the database with the demo data:
$ mise run db:reset # wipe and rebuild the database (data loss!)
$ OVERWRITE=1 mise run setup # full setup, database included
$ OVERWRITE=1 mise run server # idem, and then start the dev stack
Running the halves separately¶
$ mise run server:backend # only pserve, on http://localhost:6543
$ mise run server:frontend # only the Vite dev server, on http://localhost:5173
In development.ini, vue.mode = src (the default) makes the admin page
load its modules from the Vite dev server, so you get hot reloading. Set it to
dist to use the built assets in atramhasis/static/dist instead.
Docker services¶
Atramhasis needs no services: it ships with a SQLite development database and its test suite runs entirely on SQLite, so mise install && mise run server works without Docker.
A docker-compose.yml with a PostgreSQL container is provided for the
optional PostgreSQL setup:
$ mise run docker # start PostgreSQL (skipped if port 5432 is already in use)
$ mise run docker:down # stop it again (data is kept in the named volume)
To develop against PostgreSQL, uncomment the postgresql url in
development.ini, start the container and run the setup as usual. The
PostgreSQL driver is part of the development dependencies, and the setup creates,
migrates and seeds the database:
$ mise run docker
$ mise run setup
To switch an existing setup to PostgreSQL, run mise run db:reset instead of mise run setup. Any other PostgreSQL server works too: if something already listens on port 5432, mise run docker does not start the container and the database is created on that server instead.
Activating mise in your shell¶
$ echo 'eval "$(mise activate bash)"' >> ~/.bashrc # ~/.zshrc for zsh
The virtualenv is then activated automatically in this directory. It is also active inside mise run and mise exec.
Other tasks¶
mise run test - run the backend test suite
mise run pip-compile - regenerate the
lockfiles/requirements-dev-py*.txtfilesmise run build - build the wheel and the sdist (build:wheel / build:sdist for one of the two)
mise run cleanup - remove everything the setup creates (virtualenv, node_modules, built assets, compiled catalogs)
mise tasks always gives the complete, up-to-date list.
General installation¶
We recommend installing Atramhasis in a virtual environment.
$ python -m venv atramhasis_dev
$ . atramhasis_dev/bin/activate
# Make sure pip and pip-tools are up to date
$ pip install --upgrade pip pip-tools
To install a fully working development environment, version-specific development requirements files are provided in the lockfiles/ directory. Select the file that matches your Python version and pass it to pip sync. This will install all requirements needed for Atramhasis development, including development tools such as Sphinx and pytest.
For example:
$ pip-sync lockfiles/requirements-dev-py3.12.txt
The following step will help you get the python development environment up and running. If you also need to work on the javascript admin backend, please refer to the admin module documentation.
# Install packages in dev mode. During a pip install, the JS files will also be built.
$ pip install -e ."[dev]"
# create or update database
$ alembic upgrade head
# insert sample data
$ initialize_atramhasis_db development.ini
# generate first RDF download
$ dump_rdf development.ini
# compile the Message Catalog Files
$ pybabel compile --directory 'atramhasis/locale' --domain atramhasis --statistics true
Once you’ve executed these steps, you can run a development server. This uses the standard pyramid server (Waitress) and should not be used as-is in a production environment.
# run a local development server
$ pserve --reload development.ini
Update requirements files¶
This project does not offer requirements files that are manually maintained. The dependencies are defined within pyproject.toml. There, you can add, modify, or remove libraries. Afterward, run pip-compile to generate the requirements files.
# Update pyproject.toml and compile requirements files as follows:
$ ./scripts/compile-requirements.sh
Admin and frontend development¶
Since version 3.3.0, the admin client uses Vue 3. To work on the frontend/admin client, install Node.js and pnpm.
Required version ranges are:
Node.js: >=22.12.0
pnpm: >=9
# Verify installed versions
$ node -v
$ pnpm -v
If Node.js is not installed yet, install it first. We recommend using fnm so you can manage Node.js versions per project:
# Install Node.js (LTS) with fnm
$ fnm install --lts
$ fnm use lts-latest
If pnpm is not installed yet, you can enable it through corepack:
$ corepack enable
$ corepack prepare pnpm@latest --activate
The frontend source lives in frontend.
$ cd frontend
$ pnpm install
# Start the Vue app in dev mode
$ pnpm dev
# Type-check the Vue + TS application
$ pnpm type-check
# Build production assets
$ pnpm build
The production build outputs static assets into atramhasis/static/dist.
In development.ini, use vue.mode to control whether source (src) or
built (dist) frontend assets are used.
When building a distribution with hatch build, the build hook installs frontend dependencies and runs the frontend build automatically.
Translations¶
Translations are handled differently for backend templates and the Vue admin frontend.
Backend/template translations¶
When updating the backend templates, add translation keys with {% trans %} tags in the templates.
<h2>{% trans %}welcome_to{% endtrans %}</h2>
To update the message catalogs, do as follows:
$ pybabel extract --add-comments 'TRANSLATORS:' --output-file 'atramhasis/locale/atramhasis.pot' --width 80 --mapping-file 'message-extraction.ini' atramhasis
$ pybabel update --input-file 'atramhasis/locale/atramhasis.pot' --output-dir 'atramhasis/locale' --previous true --domain atramhasis
Update the catalogs accordingly and run:
$ pybabel compile --directory 'atramhasis/locale' --domain atramhasis --statistics true
You might also want to add a new translation. Suppose you want to add a German translation.
$ pybabel init --locale de --input-file 'atramhasis/locale/atramhasis.pot' --output-dir atramhasis/locale --domain atramhasis
Edit atramhasis/locale/de/LC_MESSAGES/atramhasis.po and add the necessary
translations. Just as with updating the catalogs, you need to recompile them.
$ pybabel compile --directory 'atramhasis/locale' --domain atramhasis --statistics true
At this moment, Atramhasis will still only show the default languages in its
language switcher. If you want to add your new language, you need to edit your
development.ini (or similar file). Look for the line that says
available_languages and add your locale identifier.
available_languages = en nl fr de
After restarting your server you will now have the option of switching to German.
Admin frontend translations (Vue i18n)¶
The Vue admin frontend uses vue-i18n with locale JSON files in frontend/src/locales.
Currently frontend/src/i18n.ts loads frontend/src/locales/en.json.
To add or update admin UI translations:
Edit translation keys in
frontend/src/locales/en.json.If adding another locale, create a new JSON file in
frontend/src/locales.Register that locale in
frontend/src/i18n.tsby importing the file and adding it to messages.Run frontend checks/build:
$ cd frontend
$ pnpm type-check
$ pnpm build
Running a Linked Data Fragments server¶
If you want to add a Linked Data Fragments server, Atramhasis makes it easy for you. First you need to decide if you want to run the server with HDT files. If not, you can always use raw Turtle files, but be aware that the HDT files offer much better performance.
If you want to use HDT files, please install hdt-cpp. Be aware that
you might have to download the source files and compile them yourself. Once
you have done so, add the rdf2hdt command to your development.ini file.
Supposing you installed it in /opt/hdt-cpp/hdt-lib/tools/rdf2hdt:
# Location of rdf2hdt executable
atramhasis.rdf2hdt = /opt/hdt-cpp/hdt-lib/tools/rdf2hdt
Now, whenever Atramhasis creates rdf dumps it will also create HDT files. If you do not have rdf2hdt installed, you will still have Turtle datadumps that can be used by the LDF-server.
$ dump_rdf development.ini
Now you’re ready to generate the configuration for the LDF server. Out of the
box this file will be generated in the same directory your
development.ini is located in, but you can override this in your ini
file by setting atramhasis.ldf.config_location or you can pass this on the
command line
# Generate config
$ generate_ldf_config development.ini
# Generate config and override config_location
$ generate_ldf_config development.ini -l /opt/my/ldf/server
Now you’re ready to run your LDF server. First we need to install it. It requires Node.js 4.0 or higher and should run on OSX and Linux. Please refer to the LDF server documentation for troubleshooting.
# Install ldf-server
$ [sudo] pnpm add -g @ldf/server
# Run ldf-server
$ ldf-server ldf_server_config.json
Now you have an LDF server running at http://localhost:3000. Browse there and have fun!
When deploying Atramhasis with an LDF server in production, we recommend runnig both behind eg. nginx. In case you want to do this, you might run Atramhasis on port 6543 and LDF server on port 3000, but serve both through nginx. You can easily do this by setting the atramhasis.ldf.baseurl in your ini file. Suppose you want to server both on the host demo.atramhasis.org with Atramhasis as the root of your domain and the LDF server at /ldf. In this case, set atramhasis.ldf.baseurl to https://demo.atramhasis.org/ldf.
Configuring the visit log¶
Every visit to a concept or conceptscheme is logged with the origin of the request:
- HTML
Browser visits to concept and conceptscheme pages. Requests without an Accept header or with Accept: */* also get the HTML page and are logged as HTML.
- REST
JSON requests, eg. applications using skosprovider_atramhasis.
- RDF
.rdf, .ttl and .jsonld requests, and content negotiation for RDF formats.
- CSV
CSV exports of a conceptscheme.
The atramhasis.audit.origins setting controls which origins are written to the concept_visit_log and conceptscheme_visit_log tables. By default all origins are logged. HTML visits are the best signal of what end users are interested in, while the other origins are mostly machine-to-machine traffic. Logging only HTML visits keeps the tables smaller and makes the most popular concepts reflect actual user interest:
atramhasis.audit.origins = HTML
An empty value logs all origins. Visits whose origin can not be determined are never logged. An unknown origin in the setting raises a configuration error at startup.
Pruning the visit logs¶
Every visit to a concept or conceptscheme adds a row to the concept_visit_log or conceptscheme_visit_log table. These rows are never removed automatically, so both tables keep growing. The concept_visit_log is used to show the most popular concepts of a conceptscheme. This only looks at a rolling period (last_day, last_week, last_month or last_year), so Atramhasis never uses visits older than one year. The conceptscheme_visit_log is only written, never read, by Atramhasis.
The prune_visit_log script removes old rows from both tables. Like the other scripts it takes the application’s ini file:
$ prune_visit_log development.ini --keep year --buffer-days 3
The following options are available:
- –keep
How much data to keep: week, month or year. Defaults to year. The default templates show the most popular concepts of the last month, so keeping only a week will make that list incomplete. The script logs a warning when –keep week is used.
- –buffer-days
Extra days kept on top of –keep. Defaults to 3.
- –tables
Which visit log tables to prune: concept, conceptscheme or both (eg. –tables concept conceptscheme). Defaults to both.
- –dry-run
Only report how many rows would be deleted, without deleting them.
All rows visited before today - <keep> - <buffer-days> are deleted. This uses the same rolling periods as the popular concepts, so a pruned table always covers the period it has to support.
The script is safe to run while the application is live. It only deletes old rows, while new visits always get a recent timestamp, and it takes no lock that blocks the application’s inserts or reads. This makes it a good candidate for a nightly cron job:
0 3 * * * /path/to/venv/bin/prune_visit_log /path/to/production.ini --keep year --buffer-days 3
The script does not make a backup or export of the deleted rows. If you need to keep them, make sure they are part of your regular database backups.
Disk space¶
A DELETE does not shrink the table or index files on disk. For regular nightly runs this is not a problem: PostgreSQL’s autovacuum makes the freed space reusable and the tables stay roughly the same size.
First run on an existing large table¶
On an instance that has been running for a long time, the first run can delete tens of millions of rows. For this one-off cleanup it is faster to copy the rows you want to keep, truncate the table and put the rows back. Run this manually during quiet hours, since TRUNCATE locks the table. On PostgreSQL:
BEGIN;
CREATE TEMP TABLE keep ON COMMIT DROP AS
SELECT * FROM concept_visit_log
WHERE visited_at >= current_date - interval '1 year 3 days';
TRUNCATE concept_visit_log;
INSERT INTO concept_visit_log SELECT * FROM keep;
COMMIT;
VACUUM ANALYZE concept_visit_log;
BEGIN;
CREATE TEMP TABLE keep ON COMMIT DROP AS
SELECT * FROM conceptscheme_visit_log
WHERE visited_at >= current_date - interval '1 year 3 days';
TRUNCATE conceptscheme_visit_log;
INSERT INTO conceptscheme_visit_log SELECT * FROM keep;
COMMIT;
VACUUM ANALYZE conceptscheme_visit_log;
Match the interval to the –keep and –buffer-days options you will use for the regular runs. TRUNCATE frees the disk space immediately, the VACUUM ANALYZE only refreshes the statistics and the visibility map. After this, the regular prune_visit_log runs only have a day’s worth of rows to delete.
Contributing¶
Atramhasis is being developed as open source software by the Flanders Heritage Agency. All development is done on the agency’s Github page for Atramhasis.
Since we place a lot of importance of code quality, we expect to have a good amount of code coverage present and run frequent unit tests. All commits and pull requests will be tested with Github Workflow Actions tests. We monitor code coverage on all pull requests using Github Actions workflows and orgoro/coverage.
Locally you can run unit tests by using pytest.
# No coverage
$ py.test
# Coverage
$ py.test --cov atramhasis --cov-report term-missing
# Only run a subset of the tests
$ py.test atramhasis/tests/test_views.py
Every pull request will be run through Github Workflow Actions tests. When providing a pull request, please run the unit tests first and make sure they all pass. Please provide new unit tests to maintain 100% coverage. If you send us a pull request and this build doesn’t function, please correct the issue at hand or let us know why it’s not working.
Distribution¶
To build a distribution for your project, you can use the hatch build command. This command will generate the necessary distribution archives, such as wheels and source distributions.
In addition to building the Python distribution, the hatch build command will also compile the JavaScript code located in the static folder. This ensures that all static assets are properly built and included in the distribution package.
$ pip install hatch
$ hatch build
Alternatively, you can specify your build as a wheel or as a source distribution (sdist) using the -t or –type parameter.
$ hatch build -t wheel
$ hatch build -t sdist