Hello,
I’m working on a project using Bokeh 3.2.1 where I have a layout with multiple plots and widgets organized in multiple columns and rows. I would like all the plots to use as much space as they can while being scaled to maintain their aspect ratio whenever I resize the browser window, but haven’t managed to do so.
Based on the documentation, I thought sizing_mode = scale_both and/or aspect_ratio = 1.0 would do the trick, but apparently not.
I tried to create a minimal example below; please note that I use pyscript, so the code below can be saved as html which can be opened in a browser to test.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<script type="text/javascript" src="https://cdn.bokeh.org/bokeh/release/bokeh-3.2.1.js"></script>
<script type="text/javascript" src="https://cdn.bokeh.org/bokeh/release/bokeh-widgets-3.2.1.min.js"></script>
<script type="text/javascript" src="https://cdn.bokeh.org/bokeh/release/bokeh-tables-3.2.1.min.js"></script>
<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/@holoviz/[email protected]/dist/panel.min.js"></script>
<link rel="stylesheet" href="https://pyscript.net/releases/2022.12.1/pyscript.css" />
<script defer src="https://pyscript.net/releases/2022.12.1/pyscript.js"></script>
</head>
<style>
html,body{
height: 100%;
min-height: 100%;
padding: 0;
margin: 0;
}
.maindiv {
height: 100%;
min-height: 100%;
padding: 0;
margin: 0;
}
</style>
<body>
<py-config>
packages = [
"https://cdn.holoviz.org/panel/1.2.1/dist/wheels/bokeh-3.2.1-py3-none-any.whl",
"https://cdn.holoviz.org/panel/1.2.1/dist/wheels/panel-1.2.1-py3-none-any.whl",
]
</py-config>
<div id="simple_app" class="maindiv"></div>
<py-script>
import panel as pn
pn.extension()
from bokeh.plotting import figure
from bokeh.layouts import column, row
from bokeh.models import Div
p1 = figure(toolbar_location=None, aspect_ratio=1.0, sizing_mode="scale_both")
p1.line([0, 1], [0, 1])
div1 = Div(text='', width=500)
bokeh_app = pn.pane.Bokeh(row(column(p1, sizing_mode="scale_both"), column(div1, background='red'), sizing_mode="stretch_both"))
pn.Row(bokeh_app).servable(target='simple_app')
</py-script>
</body>
</html>
Basically, with the above, the aspect ratio of the plot is not maintained, the plot just stretches both horizontally and vertically.
When I set sizing_mode=“scale_width” for the plot instead, the aspect ratio is maintained if the parent column is higher than it is wide; however, when I resize the browser window such that the parent column is wider than it is high, then the aspect ratio is again not maintained and the plot uses as much width as possible whereas the height is being reduced.
The behavior I would expect is that, if the parent column is wider than it is high, the plot would be scaled such that the maximum height is being used and the width scaled such that the aspect ratio is maintained (i.e., the width will be scaled to match the height to maintain an aspect ratio of 1.0).
I hope I’m just missing something trivial!