Graph with text nodes: how to display text in glyph

Hello,
I started a project with some days ago.
What I try to do is to build a network graph. I want to have nodes with a glyph which have a text and the size adapted to the text. The change for each node.
What I have already done is to use Rect as glyph and to add label with Labelset. I specified a width for the glyph which depends of the text length, and added a x offset for the label. This works when the graph width is fixed, but when the width is not what I have tested, text can go out of the rectangle. Thus, this doesn’t seems to be a proper way.
I also tried Text instead of Rect, but I didn’t see any way to add background or surrounding rectangle. It doesn’t seems neither that css properties can be added.
Do you see a good way to do what I expect?

@papoteur-mga

Does this mean that things don’t work the way you want if (a) the axes ranges of the plot change or (b) the browser window is resized?

Two things you might experiment with are

(*) setting the width_units and/or height_units of the rectangles to 'screen' and do your calculations based on those.

(*) using LabelSets only without the Rect glyphs. If you only care about having rectangular bounding boxes with text inside, you can set background fill and border-line properties of the labels. E.g.

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
"""
import numpy as np

from bokeh.plotting import figure, show
from bokeh.models import ColumnDataSource, LabelSet

x = np.random.randn(50)
y = np.random.randn(50)
text = ['Node {:}'.format(i) for i,_ in enumerate(x)]

data = dict(x=x, y=y, text=text)
source = ColumnDataSource(data=data)

p = figure(width=400, height=300, x_range=(-2.0,2.0), y_range=(-2.0,2.0))

ll = LabelSet(x='x', y='y', text='text', source=source,
              text_font_size='8pt',
              background_fill_color='#ff0000', background_fill_alpha=0.2,
              border_line_color='#000000', border_line_alpha=1.0)

p.add_layout(ll)

show(p)

Oh yes, the second solution is what I look for.
I still use x_offset and y_offset to have the label quite centered.