Skip to content

Commit 7f59a21

Browse files
authored
Merge pull request #69 from metabase/minimal-examples
Minimal examples
2 parents 6c422c7 + fd4c891 commit 7f59a21

File tree

4 files changed

+123
-0
lines changed

4 files changed

+123
-0
lines changed

django/minimal_example/index.html

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<title>{{ title }}</title>
5+
</head>
6+
<body>
7+
<h1>Embed {{ title }}</h1>
8+
<iframe
9+
src="{{iframeUrl}}"
10+
frameborder="1"
11+
width="800"
12+
height="600"
13+
></iframe>
14+
</body>
15+
</html>

django/minimal_example/index.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# A minimal example of a signed embedding in a Django application. Run this program with:
2+
#
3+
# django-admin runserver --pythonpath=. --settings=index
4+
#
5+
# URLs are:
6+
# - http://localhost:8000/ : home page
7+
#
8+
# You will need to modify the value of `METABASE_SECRET_KEY` to make this work.
9+
# Please see https://www.metabase.com/learn/developing-applications/advanced-metabase/embedding-charts-and-dashboards.html
10+
# for the full tutorial.
11+
12+
import os
13+
import time
14+
15+
from django.conf.urls import url
16+
from django.http import HttpResponse
17+
from django.template.loader import render_to_string
18+
19+
import jwt
20+
21+
22+
DEBUG = True
23+
SECRET_KEY = '4l0ngs3cr3tstr1ngw3lln0ts0l0ngw41tn0w1tsl0ng3n0ugh'
24+
ROOT_URLCONF = __name__
25+
TEMPLATES = [{
26+
'BACKEND': 'django.template.backends.django.DjangoTemplates',
27+
'DIRS': [os.getcwd()]
28+
}]
29+
30+
31+
def home(request):
32+
METABASE_SITE_URL = 'http://localhost:3000'
33+
METABASE_SECRET_KEY = '40e0106db5156325d600c37a5e077f44a49be1db9d02c96271e7bd67cc9529fa'
34+
payload = {
35+
'resource': {'question': 1},
36+
'params': {},
37+
'exp': round(time.time()) + (60 * 10)
38+
}
39+
token = jwt.encode(payload, METABASE_SECRET_KEY, algorithm='HS256')
40+
iframeUrl = METABASE_SITE_URL + '/embed/question/' + token + '#bordered=true&titled=true'
41+
42+
html = render_to_string('index.html', {
43+
'title': 'Embedding Metabase',
44+
'iframeUrl': iframeUrl
45+
})
46+
return HttpResponse(html)
47+
48+
49+
urlpatterns = [
50+
url(r'^$', home, name='homepage')
51+
]

html/index.html

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<!--
2+
A simple example showing how to embed a question using an iframe. This embed is not signed; please see:
3+
4+
https://www.metabase.com/learn/developing-applications/advanced-metabase/embedding-charts-and-dashboards.html
5+
6+
for the full tutorial.
7+
8+
You will need to modify the `src` attribute of the iframe to match your Metabase installation.
9+
-->
10+
<html>
11+
<head>
12+
<title>Metabase Embedding</title>
13+
</head>
14+
<body>
15+
<h1>Metabase Embedding</h1>
16+
<iframe
17+
src="http://localhost:3000/public/question/7f0c3c4b-173e-452c-810f-ccb3fc4482a3"
18+
frameborder="1"
19+
width="600"
20+
height="400"
21+
></iframe>
22+
</body>
23+
</html>

shiny/minimal_example/app.R

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
library(shiny)
2+
library(jose)
3+
library(glue)
4+
5+
# Metabase location and secret key of our embedded question.
6+
METABASE_SITE_URL <- 'http://localhost:3000'
7+
METABASE_SECRET_KEY <- '40e0106db5156325d600c37a5e077f44a49be1db9d02c96271e7bd67cc9529fa'
8+
9+
# Display title and iframe in UI.
10+
ui <- bootstrapPage(
11+
h1('Page title'),
12+
uiOutput('container')
13+
)
14+
15+
# Server does the work.
16+
server <- function(input, output) {
17+
# Token expires 10 minutes in the future.
18+
expiry <- as.integer(unclass(Sys.time())) + (60 * 10)
19+
20+
# Construct params in two steps so that JSON conversion knows it's a list.
21+
params <- list()
22+
names(params) <- character(0)
23+
24+
# Create the JWT claim.
25+
claim <- jwt_claim(exp = expiry, resource = list(question = 1), params = params)
26+
27+
# Encode token and use it to construct iframe URL.
28+
token <- jwt_encode_hmac(claim, secret = METABASE_SECRET_KEY)
29+
url <- glue("{METABASE_SITE_URL}/embed/question/{token}#bordered=true&titled=true")
30+
output$container <- renderUI(tags$iframe(width = "600", height = "600", src = url))
31+
}
32+
33+
# Run application.
34+
shinyApp(ui = ui, server = server, options = list(port = 8001))

0 commit comments

Comments
 (0)