Skip to content

Commit

Permalink
🔨 (notebook) add example of Metabase API (#134)
Browse files Browse the repository at this point in the history
🔨 (notebook) add example of Metabase API

Test of simple Metabase API (PR #134)

* Update src/notebook/example.md

* code review

use .env variables and docker metadata

---------

Co-authored-by: philippe thomy <[email protected]>
Co-authored-by: Julien Maupetit <[email protected]>
  • Loading branch information
3 people authored Jul 26, 2024
1 parent cbc386a commit 176cb4a
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
1 change: 1 addition & 0 deletions env.d/notebook
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
DATABASE_URL=postgresql+psycopg://qualicharge:pass@postgresql:5432/qualicharge-api
ADMIN_KEY="mb_44Wb2RiqYP226DCOuP6itQRqxQvxVBM+T2wW3ZlSEX8="
35 changes: 35 additions & 0 deletions src/notebook/example.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ database_url = os.getenv("DATABASE_URL")

# Create a database engine that will be used to generate connections
engine = create_engine(database_url)
database_url
```

## Fetch data from the database
Expand Down Expand Up @@ -143,3 +144,37 @@ query = 'SELECT * FROM "IDepartmentDynamic" WHERE department = 75'
paris = pd.read_sql_query(query, engine)
paris
```

## Metabase API


A large list of API endpoints is available in the [documentation](https://www.metabase.com/learn/administration/metabase-api).
See examples below.

```python
import requests
import pandas as pd

# Get metabase key from the environment
admin_key = os.getenv("ADMIN_KEY")

# Metabase server
METABASE_HOST = "metabase"
METABASE_PORT = 3000

# doc API : http://localhost:3000/api/docs
headers = {'x-api-key': API_ADMIN_KEY}
response = requests.get(f"http://{METABASE_HOST}:{METABASE_PORT}/api/permissions/group", headers=headers)
response.json()
```

Example : Search of Questions(card)

```python
response = requests.get(f"http://{METABASE_HOST}:{METABASE_PORT}/api/card", headers=headers)
cards = response.json()

# list of cards
my_card = [card for card in response if card['description'] and card['description'][:4] == 'test']
{card['id']: card['description'] for card in my_card}
```

0 comments on commit 176cb4a

Please sign in to comment.