Contributing to Documentation

The latest documentation and tutorial are available at https://superset.apache.org/.

The documentation site is built using Docusaurus 2, a modern static website generator, the source for which resides in ./docs.

Local Development

To set up a local development environment with hot reloading for the documentation site:

  1. cd docs
  2. yarn install # Installs NPM dependencies
  3. yarn start # Starts development server at http://localhost:3000

Build

To create and serve a production build of the documentation site:

  1. yarn build
  2. yarn serve

Deployment

Commits to master trigger a rebuild and redeploy of the documentation site. Submit pull requests that modify the documentation with the docs: prefix.

Creating Visualization Plugins

Visualizations in Superset are implemented in JavaScript or TypeScript. Superset comes preinstalled with several visualizations types (hereafter “viz plugins”) that can be found under the superset-frontend/plugins directory. Viz plugins are added to the application in the superset-frontend/src/visualizations/presets/MainPreset.js. The Superset project is always happy to review proposals for new high quality viz plugins. However, for highly custom viz types it is recommended to maintain a fork of Superset, and add the custom built viz plugins by hand.

Note: Additional community-generated resources about creating and deploying custom visualization plugins can be found on the Superset Wiki

Prerequisites

In order to create a new viz plugin, you need the following:

  • Run MacOS or Linux (Windows is not officially supported, but may work)
  • Node.js 16
  • npm 7 or 8

A general familiarity with React and the npm/Node system is also recommended.

Creating a simple Hello World viz plugin

To get started, you need the Superset Yeoman Generator. It is recommended to use the version of the template that ships with the version of Superset you are using. This can be installed by doing the following:

  1. npm i -g yo
  2. cd superset-frontend/packages/generator-superset
  3. npm i
  4. npm link

After this you can proceed to create your viz plugin. Create a new directory for your viz plugin with the prefix superset-plugin-chart and run the Yeoman generator:

  1. mkdir /tmp/superset-plugin-chart-hello-world
  2. cd /tmp/superset-plugin-chart-hello-world

Initialize the viz plugin:

  1. yo @superset-ui/superset

After that the generator will ask a few questions (the defaults should be fine):

  1. $ yo @superset-ui/superset
  2. _-----_ ╭──────────────────────────╮
  3. | | Welcome to the
  4. |--(o)--| generator-superset
  5. `---------´ │ generator! │
  6. ( _´U`_ ) ╰──────────────────────────╯
  7. /___A___\ /
  8. | ~ |
  9. __'.___.'__
  10. ´ ` |° ´ Y `
  11. ? Package name: superset-plugin-chart-hello-world
  12. ? Description: Hello World
  13. ? What type of chart would you like? Time-series chart
  14. create package.json
  15. create .gitignore
  16. create babel.config.js
  17. create jest.config.js
  18. create README.md
  19. create tsconfig.json
  20. create src/index.ts
  21. create src/plugin/buildQuery.ts
  22. create src/plugin/controlPanel.ts
  23. create src/plugin/index.ts
  24. create src/plugin/transformProps.ts
  25. create src/types.ts
  26. create src/SupersetPluginChartHelloWorld.tsx
  27. create test/index.test.ts
  28. create test/__mocks__/mockExportString.js
  29. create test/plugin/buildQuery.test.ts
  30. create test/plugin/transformProps.test.ts
  31. create types/external.d.ts
  32. create src/images/thumbnail.png

To build the viz plugin, run the following commands:

  1. npm i --force
  2. npm run build

Alternatively, to run the viz plugin in development mode (=rebuilding whenever changes are made), start the dev server with the following command:

  1. npm run dev

To add the package to Superset, go to the superset-frontend subdirectory in your Superset source folder run

  1. npm i -S /tmp/superset-plugin-chart-hello-world

If you publish your package to npm, you can naturally install directly from there, too. After this edit the superset-frontend/src/visualizations/presets/MainPreset.js and make the following changes:

  1. import { SupersetPluginChartHelloWorld } from 'superset-plugin-chart-hello-world';

to import the viz plugin and later add the following to the array that’s passed to the plugins property:

  1. new SupersetPluginChartHelloWorld().configure({ key: 'ext-hello-world' }),

After that the viz plugin should show up when you run Superset, e.g. the development server:

  1. npm run dev-server

Testing

Python Testing

All python tests are carried out in tox a standardized testing framework. All python tests can be run with any of the tox environments, via,

  1. tox -e <environment>

For example,

  1. tox -e py38

Alternatively, you can run all tests in a single file via,

  1. tox -e <environment> -- tests/test_file.py

or for a specific test via,

  1. tox -e <environment> -- tests/test_file.py::TestClassName::test_method_name

Note that the test environment uses a temporary directory for defining the SQLite databases which will be cleared each time before the group of test commands are invoked.

There is also a utility script included in the Superset codebase to run python integration tests. The readme can be found here

To run all integration tests for example, run this script from the root directory:

  1. scripts/tests/run.sh

You can run unit tests found in ‘./tests/unit_tests’ for example with pytest. It is a simple way to run an isolated test that doesn’t need any database setup

  1. pytest ./link_to_test.py

Testing with local Presto connections

If you happen to change db engine spec for Presto/Trino, you can run a local Presto cluster with Docker:

  1. docker run -p 15433:15433 starburstdata/presto:350-e.6

Then update SUPERSET__SQLALCHEMY_EXAMPLES_URI to point to local Presto cluster:

  1. export SUPERSET__SQLALCHEMY_EXAMPLES_URI=presto://localhost:15433/memory/default

Frontend Testing

We use Jest and Enzyme to test TypeScript/JavaScript. Tests can be run with:

  1. cd superset-frontend
  2. npm run test

To run a single test file:

  1. npm run test -- path/to/file.js

e2e Integration Testing

We use Cypress for end-to-end integration tests. One easy option to get started quickly is to leverage tox to run the whole suite in an isolated environment.

  1. tox -e cypress

Alternatively, you can go lower level and set things up in your development environment by following these steps:

First set up a python/flask backend:

  1. export SUPERSET_CONFIG=tests.integration_tests.superset_test_config
  2. export SUPERSET_TESTENV=true
  3. export CYPRESS_BASE_URL="http://localhost:8081"
  4. superset db upgrade
  5. superset load_test_users
  6. superset init
  7. superset load-examples --load-test-data
  8. superset run --port 8081

In another terminal, prepare the frontend and run Cypress tests:

  1. cd superset-frontend
  2. npm run build-instrumented
  3. cd cypress-base
  4. npm install
  5. # run tests via headless Chrome browser (requires Chrome 64+)
  6. npm run cypress-run-chrome
  7. # run tests from a specific file
  8. npm run cypress-run-chrome -- --spec cypress/e2e/explore/link.test.ts
  9. # run specific file with video capture
  10. npm run cypress-run-chrome -- --spec cypress/e2e/dashboard/index.test.js --config video=true
  11. # to open the cypress ui
  12. npm run cypress-debug
  13. # to point cypress to a url other than the default (http://localhost:8088) set the environment variable before running the script
  14. # e.g., CYPRESS_BASE_URL="http://localhost:9000"
  15. CYPRESS_BASE_URL=<your url> npm run cypress open

See superset-frontend/cypress_build.sh.

As an alternative you can use docker compose environment for testing:

Make sure you have added below line to your /etc/hosts file: 127.0.0.1 db

If you already have launched Docker environment please use the following command to assure a fresh database instance: docker compose down -v

Launch environment:

CYPRESS_CONFIG=true docker compose up

It will serve backend and frontend on port 8088.

Run Cypress tests:

  1. cd cypress-base
  2. npm install
  3. npm run cypress open

Debugging Server App

Follow these instructions to debug the Flask app running inside a docker container.

First add the following to the ./docker-compose.yaml file

  1. superset:
  2. env_file: docker/.env
  3. image: *superset-image
  4. container_name: superset_app
  5. command: ["/app/docker/docker-bootstrap.sh", "app"]
  6. restart: unless-stopped
  7. + cap_add:
  8. + - SYS_PTRACE
  9. ports:
  10. - 8088:8088
  11. + - 5678:5678
  12. user: "root"
  13. depends_on: *superset-depends-on
  14. volumes: *superset-volumes
  15. environment:
  16. CYPRESS_CONFIG: "${CYPRESS_CONFIG}"

Start Superset as usual

  1. docker compose up

Install the required libraries and packages to the docker container

Enter the superset_app container

  1. docker exec -it superset_app /bin/bash
  2. root@39ce8cf9d6ab:/app#

Run the following commands inside the container

  1. apt update
  2. apt install -y gdb
  3. apt install -y net-tools
  4. pip install debugpy

Find the PID for the Flask process. Make sure to use the first PID. The Flask app will re-spawn a sub-process every time you change any of the python code. So it’s important to use the first PID.

  1. ps -ef
  2. UID PID PPID C STIME TTY TIME CMD
  3. root 1 0 0 14:09 ? 00:00:00 bash /app/docker/docker-bootstrap.sh app
  4. root 6 1 4 14:09 ? 00:00:04 /usr/local/bin/python /usr/bin/flask run -p 8088 --with-threads --reload --debugger --host=0.0.0.0
  5. root 10 6 7 14:09 ? 00:00:07 /usr/local/bin/python /usr/bin/flask run -p 8088 --with-threads --reload --debugger --host=0.0.0.0

Inject debugpy into the running Flask process. In this case PID 6.

  1. python3 -m debugpy --listen 0.0.0.0:5678 --pid 6

Verify that debugpy is listening on port 5678

  1. netstat -tunap
  2. Active Internet connections (servers and established)
  3. Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
  4. tcp 0 0 0.0.0.0:5678 0.0.0.0:* LISTEN 462/python
  5. tcp 0 0 0.0.0.0:8088 0.0.0.0:* LISTEN 6/python

You are now ready to attach a debugger to the process. Using VSCode you can configure a launch configuration file .vscode/launch.json like so.

  1. {
  2. "version": "0.2.0",
  3. "configurations": [
  4. {
  5. "name": "Attach to Superset App in Docker Container",
  6. "type": "python",
  7. "request": "attach",
  8. "connect": {
  9. "host": "127.0.0.1",
  10. "port": 5678
  11. },
  12. "pathMappings": [
  13. {
  14. "localRoot": "${workspaceFolder}",
  15. "remoteRoot": "/app"
  16. }
  17. ]
  18. },
  19. ]
  20. }

VSCode will not stop on breakpoints right away. We’ve attached to PID 6 however it does not yet know of any sub-processes. In order to “wakeup” the debugger you need to modify a python file. This will trigger Flask to reload the code and create a new sub-process. This new sub-process will be detected by VSCode and breakpoints will be activated.

Debugging Server App in Kubernetes Environment

To debug Flask running in POD inside kubernetes cluster. You’ll need to make sure the pod runs as root and is granted the SYS_TRACE capability.These settings should not be used in production environments.

  1. securityContext:
  2. capabilities:
  3. add: ["SYS_PTRACE"]

See (set capabilities for a container)[https://kubernetes.io/docs/tasks/configure-pod-container/security-context/#set-capabilities-for-a-container] for more details.

Once the pod is running as root and has the SYS_PTRACE capability it will be able to debug the Flask app.

You can follow the same instructions as in the docker-compose. Enter the pod and install the required library and packages; gdb, netstat and debugpy.

Often in a Kubernetes environment nodes are not addressable from outside the cluster. VSCode will thus be unable to remotely connect to port 5678 on a Kubernetes node. In order to do this you need to create a tunnel that port forwards 5678 to your local machine.

  1. kubectl port-forward pod/superset-<some random id> 5678:5678

You can now launch your VSCode debugger with the same config as above. VSCode will connect to to 127.0.0.1:5678 which is forwarded by kubectl to your remote kubernetes POD.

Storybook

Superset includes a Storybook to preview the layout/styling of various Superset components, and variations thereof. To open and view the Storybook:

  1. cd superset-frontend
  2. npm run storybook

When contributing new React components to Superset, please try to add a Story alongside the component’s jsx/tsx file.

Contributing Translations

We use Flask-Babel to translate Superset. In Python files, we use the following translation functions from Flask-Babel:

  • gettext and lazy_gettext (usually aliased to _): for translating singular strings.
  • ngettext: for translating strings that might become plural.
  1. from flask_babel import lazy_gettext as _

then wrap the translatable strings with it, e.g. _('Translate me'). During extraction, string literals passed to _ will be added to the generated .po file for each language for later translation.

At runtime, the _ function will return the translation of the given string for the current language, or the given string itself if no translation is available.

In TypeScript/JavaScript, the technique is similar: we import t (simple translation), tn (translation containing a number).

  1. import { t, tn } from "@superset-ui/translation";

Enabling language selection

Add the LANGUAGES variable to your superset_config.py. Having more than one option inside will add a language selection dropdown to the UI on the right side of the navigation bar.

  1. LANGUAGES = {
  2. 'en': {'flag': 'us', 'name': 'English'},
  3. 'fr': {'flag': 'fr', 'name': 'French'},
  4. 'zh': {'flag': 'cn', 'name': 'Chinese'},
  5. }

Creating a new language dictionary

First check if the language code for your target language already exists. Check if the two letter ISO 639-1 code for your target language already exists in the superset/translations directory:

  1. ls superset/translations | grep -E "^[a-z]{2}\/"

If your language already has a preexisting translation, skip to the next section

The following languages are already supported by Flask AppBuilder, and will make it easier to translate the application to your target language: Flask AppBuilder i18n documentation

To create a dictionary for a new language, first make sure the necessary dependencies are installed:

  1. pip install -r superset/translations/requirements.txt

Then run the following, where LANGUAGE_CODE is replaced with the language code for your target language:

  1. pybabel init -i superset/translations/messages.pot -d superset/translations -l LANGUAGE_CODE

For instance, to add a translation for Finnish (language code fi), run the following:

  1. pybabel init -i superset/translations/messages.pot -d superset/translations -l fi

Extracting new strings for translation

Periodically, when working on translations, we need to extract the strings from both the backend and the frontend to compile a list of all strings to be translated. It doesn’t happen automatically and is a required step to gather the strings and get them into the .po files where they can be translated, so that they can then be compiled.

This script does just that:

  1. ./scripts/translations/babel_update.sh

Updating language files

Run the following command to update the language files with the new extracted strings.

  1. pybabel update -i superset/translations/messages.pot -d superset/translations --ignore-obsolete

You can then translate the strings gathered in files located under superset/translation, where there’s one folder per language. You can use Poedit to translate the po file more conveniently. Here is a tutorial.

To perform the translation on MacOS, you can install poedit via Homebrew:

  1. brew install poedit

After this, just start the poedit application and open the messages.po file. In the case of the Finnish translation, this would be superset/translations/fi/LC_MESSAGES/messages.po.

Applying translations

To make the translations available on the frontend, we need to convert the PO file into a collection of JSON files. To convert all PO files to formatted JSON files you can use the build-translation script

  1. npm run build-translation

Finally, for the translations to take effect we need to compile translation catalogs into binary MO files for the backend using pybabel.

  1. pybabel compile -d superset/translations

Linting

Python

We use Pylint for linting which can be invoked via:

  1. # for python
  2. tox -e pylint

In terms of best practices please avoid blanket disabling of Pylint messages globally (via .pylintrc) or top-level within the file header, albeit there being a few exceptions. Disabling should occur inline as it prevents masking issues and provides context as to why said message is disabled.

Additionally, the Python code is auto-formatted using Black which is configured as a pre-commit hook. There are also numerous editor integrations

TypeScript

  1. cd superset-frontend
  2. npm ci
  3. # run eslint checks
  4. npm run eslint -- .
  5. # run tsc (typescript) checks
  6. npm run type

If using the eslint extension with vscode, put the following in your workspace settings.json file:

  1. "eslint.workingDirectories": [
  2. "superset-frontend"
  3. ]