Metabase is an open-source tool used for creating data dashboards and visualizations. It simplifies the process of exploring and understanding your data. By providing a user-friendly interface, it allows users to query data without needing extensive SQL knowledge. Metabase supports a variety of databases and has a robust API.
Metabase is a powerful open-source tool for creating data dashboards and visualizations. It offers a RESTful API, allowing programmatic access and modification of many of its features.
To make use of this script, a local Metabase server needs to be set up. This process involves Docker, which needs to be installed and configured on your system.
Follow these steps to set up Metabase locally.
Before setting up Metabase locally, you need to have Docker installed on your system as we will be using it for the setup.
-
If Docker is not already installed on your system, you can download it from the official Docker website. Here are the links:
-
After installing Docker, you can check whether it's been installed correctly by opening a terminal and running the following command:
docker --version
-
Pull the Metabase image from Docker Hub
- Open a terminal and run the following command:
docker pull metabase/metabase
- Open a terminal and run the following command:
-
Run the Metabase Docker container
- Run the command:
docker run -d -p 3000:3000 --name metabase metabase/metabase
- Run the command:
-
Access Metabase
- Open your browser and visit
http://localhost:3000
- You will be greeted with the Metabase setup wizard. Follow the prompts to set up an account and connect your data sources.
- Open your browser and visit
-
Create and Save a Native Question
- In Metabase, create a new 'question' (a query or report), and save it to your 'Personal Collection' or any other collection.
Ex: Fetch users details for specific emails from
people
table of Sample Databaseselect email, password, name from people where ( email in ( 'borer-hudson@yahoo.com', 'williamson-domenica@yahoo.com' ) [[and email = {{email}}]] -- email parameter is optional )
-
Run Query
- Once you've run the query, you should see the results as shown in the image below
Before you begin, make sure you have Python3 and pip3 installed on your system. If not, refer to the official Python installation guide and pip installation guide.
Follow the steps below to install the metabase-api-python
package:
NOTE: We have not yet handled all error cases, and we have only added a few functions. More functions and scripts will be added over time. Please keep this in mind while using this module.
-
Open a terminal.
-
If you're using a virtual environment (which is a good practice), make sure to activate it. If you're not, that's perfectly fine too.
-
Use pip to install the
metabase-api-python
package from PyPi:
pip3 install metabase-api-python
- Now, you can use the
metabase-api-python
package in your Python scripts. Here's a basic example:
from metabase_api_python import MetabaseAPI
metabase_api = MetabaseAPI(
base_url="http://localhost:3000",
user_name="YOUR_USERNAME",
password="YOUR_PASSWORD"
)
print("Access Token is : ", metabase_api.access_token)
Please replace YOUR_USERNAME
and YOUR_PASSWORD
with your actual Metabase credentials.
Remember, always keep these credentials secure. Do not expose them in any places.
Note: The installation process might vary slightly depending on your operating system and the specific configuration of your Python environment.
- get_data_from_question
- archive_question
- delete_question
Ex: Get the question data without parameters
from metabase_api_python import MetabaseAPI
metabase_api = MetabaseAPI(
base_url="http://localhost:3000",
user_name="xxxxxx@gmail.com",
password="xxxxxx"
)
# Get Question Reponse
question_response = metabase_api.get_data_from_question(
question_id=1
)
print(question_response)
Note: Usually, the question_id for the first question you create in Metabase is 1. You can verify this by checking the URL when you navigate to the question in your local Metabase interface. For example, if your URL is http://localhost:3000/question/1-your-question-name, the question_id is 1.
Ex: Get the question data with parameters
payload = [
{
"type": "text",
"value": "borer-hudson@yahoo.com",
"target": ["variable", ["template-tag", "email"]]
}
]
question_response = metabase_api.get_data_from_question(
question_id=1,params=payload
)
print(question_response)
Ex: Archive Question
# Archive Question
metabase_api.archive_question(
question_id=1
)