forked from bokeh/bokeh
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix rendering on JupyterHub 4.1: respect
absolute_url
for protocol …
…determination, support `data-absolute-url` (bokeh#14003) * Fix rendering on JupyterHub 4.1 * Get rid of the mutable variable Co-authored-by: Mateusz Paprocki <[email protected]> * Add an example * Do not use type casting, define a custom type guard * Reduce size of the negative example iframe * Lint * Move to `app/iframe_embed` * Skip `iframe_embed` for now * Retrigger CI * Move to `examples/server/api` --------- Co-authored-by: Mateusz Paprocki <[email protected]>
- Loading branch information
1 parent
3a74368
commit 0300cc4
Showing
5 changed files
with
111 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
This example demonstrates embedding in an iframe using srcdoc under restrictive CSP. | ||
|
||
To view the example, run: | ||
|
||
python main.py | ||
|
||
in this directory, and navigate to: | ||
|
||
http://localhost:5000 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import numpy as np | ||
|
||
from bokeh.io import curdoc | ||
from bokeh.plotting import figure | ||
|
||
N = 4000 | ||
x = np.random.random(size=N) * 100 | ||
y = np.random.random(size=N) * 100 | ||
radii = np.random.random(size=N) * 1.5 | ||
|
||
p = figure(tools="", toolbar_location=None) | ||
p.circle(x, y, radius=radii, fill_alpha=0.6) | ||
|
||
curdoc().add_root(p) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
'''This example demonstrates embedding in an iframe using srcdoc under restrictive CSP. | ||
To view the example, run: | ||
python main.py | ||
in this directory, and navigate to: | ||
http://localhost:5000 | ||
''' | ||
import atexit | ||
import subprocess | ||
|
||
from flask import Flask, render_template_string | ||
|
||
from bokeh.client import pull_session | ||
from bokeh.embed.server import server_html_page_for_session | ||
from bokeh.resources import INLINE | ||
|
||
app_html = """ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<body> | ||
<p> | ||
This is an example of cross-origin embedding using an iframe under restrictive Content Security Policy (CSP). | ||
Under strict CSP iframe embedding with `src` does not work: | ||
</p> | ||
<iframe src="{{ app_url }}" width=100% height=50px></iframe> | ||
<p>But it is still possible to embed with `srcdoc` attribute and using `data-absolute-url`:</p> | ||
<iframe id="myiframe" width=100% height=500px></iframe> | ||
<script> | ||
const iframe = document.querySelector("#myiframe"); | ||
iframe.dataset.absoluteUrl = {{ app_url|tojson }}; | ||
iframe.srcdoc = {{ code|tojson }}; | ||
</script> | ||
</body> | ||
</html> | ||
""" | ||
|
||
app = Flask(__name__) | ||
|
||
bokeh_process = subprocess.Popen( | ||
['python', '-m', 'bokeh', 'serve', '--port=5151', '--allow-websocket-origin=localhost:5000', '--allow-websocket-origin=127.0.0.1:5000', 'bokeh_server.py'], | ||
stdout=subprocess.PIPE, | ||
) | ||
|
||
@atexit.register | ||
def kill_server(): | ||
bokeh_process.kill() | ||
|
||
|
||
@app.after_request | ||
def add_security_headers(resp): | ||
resp.headers["Content-Security-Policy"] = "frame-ancestors 'none'" | ||
return resp | ||
|
||
@app.route('/') | ||
def home(): | ||
app_url = "http://localhost:5151/bokeh_server" | ||
with pull_session(url=app_url) as session: | ||
code = server_html_page_for_session(session=session, resources=INLINE, title='test') | ||
return render_template_string(app_html, code=code, app_url=app_url) | ||
|
||
|
||
if __name__ == '__main__': | ||
app.run() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters