GSP Network API Reference¶
The GSP Network module provides network-based rendering capabilities, enabling remote visualization and client-server architectures.
Overview¶
gsp_network
¶
gsp_network provides functionality for rendering graphics over a network using different remote renderers.
Renderer Module¶
The renderer module contains the network renderer implementation for remote rendering.
gsp_network.renderer
¶
Network renderer that sends rendering requests to a remote server and displays the results using Matplotlib.
NetworkRenderer
¶
Bases: gsp.types.renderer_base.RendererBase
Note: this requires a running gsp_network server. See the README for instructions.
IMPORTANT: it DOES NOT depend on GSP matplotlib renderer, it only uses pip matplotlib to display the remotely rendered images.
Source code in src/gsp_network/renderer/network_renderer.py
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 | |
__init__(canvas: Canvas, server_base_url: str, remote_renderer_name: Literal['matplotlib', 'datoviz'] = 'matplotlib') -> None
¶
Initialize the NetworkRenderer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
canvas
|
gsp.core.canvas.Canvas
|
description |
required |
server_base_url
|
str
|
description |
required |
remote_renderer_name
|
typing.Literal['matplotlib', 'datoviz']
|
description. Defaults to "matplotlib". |
'matplotlib'
|
Source code in src/gsp_network/renderer/network_renderer.py
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 | |
get_canvas() -> Canvas
¶
Get the canvas associated with the network renderer.
Returns:
| Name | Type | Description |
|---|---|---|
Canvas |
gsp.core.canvas.Canvas
|
The canvas associated with the network renderer. |
Source code in src/gsp_network/renderer/network_renderer.py
71 72 73 74 75 76 77 | |
close() -> None
¶
Close the network renderer and release resources.
Source code in src/gsp_network/renderer/network_renderer.py
79 80 81 82 83 84 85 | |
get_remote_renderer_name() -> Literal['matplotlib', 'datoviz']
¶
Get the name of the remote renderer being used.
Returns:
| Type | Description |
|---|---|
typing.Literal['matplotlib', 'datoviz']
|
Literal["matplotlib", "datoviz"]: The name of the remote renderer. |
Source code in src/gsp_network/renderer/network_renderer.py
87 88 89 90 91 92 93 | |
render(viewports: Sequence[Viewport], visuals: Sequence[VisualBase], model_matrices: Sequence[TransBuf], cameras: Sequence[Camera]) -> bytes
¶
Render the scene remotely and update the matplotlib figure with the rendered image.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
viewports
|
typing.Sequence[gsp.core.viewport.Viewport]
|
The viewports to render. |
required |
visuals
|
typing.Sequence[gsp.types.visual_base.VisualBase]
|
The visuals to render. |
required |
model_matrices
|
typing.Sequence[gsp.types.transbuf.TransBuf]
|
The model matrices for the visuals. |
required |
cameras
|
typing.Sequence[gsp.core.camera.Camera]
|
The cameras to use for rendering. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
bytes |
bytes
|
The rendered image data in PNG format. |
Raises:
| Type | Description |
|---|---|
Exception
|
If the network request fails. |
Source code in src/gsp_network/renderer/network_renderer.py
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 | |
show() -> None
¶
Show the rendered canvas (blocking call).
Source code in src/gsp_network/renderer/network_renderer.py
157 158 159 160 161 162 163 164 | |
get_mpl_figure() -> matplotlib.figure.Figure
¶
Get the underlying Matplotlib figure.
Returns:
| Type | Description |
|---|---|
matplotlib.figure.Figure
|
matplotlib.figure.Figure: The Matplotlib figure used by the renderer. |
Source code in src/gsp_network/renderer/network_renderer.py
166 167 168 169 170 171 172 | |
Network Renderer¶
gsp_network.renderer.network_renderer
¶
Network renderer that sends rendering requests to a remote server and displays the results using Matplotlib.
NetworkPayload
¶
Bases: typing.TypedDict
Type definition for the network payload sent to the server.
Source code in src/gsp_network/renderer/network_renderer.py
31 32 33 34 | |
NetworkRenderer
¶
Bases: gsp.types.renderer_base.RendererBase
Note: this requires a running gsp_network server. See the README for instructions.
IMPORTANT: it DOES NOT depend on GSP matplotlib renderer, it only uses pip matplotlib to display the remotely rendered images.
Source code in src/gsp_network/renderer/network_renderer.py
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 | |
__init__(canvas: Canvas, server_base_url: str, remote_renderer_name: Literal['matplotlib', 'datoviz'] = 'matplotlib') -> None
¶
Initialize the NetworkRenderer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
canvas
|
gsp.core.canvas.Canvas
|
description |
required |
server_base_url
|
str
|
description |
required |
remote_renderer_name
|
typing.Literal['matplotlib', 'datoviz']
|
description. Defaults to "matplotlib". |
'matplotlib'
|
Source code in src/gsp_network/renderer/network_renderer.py
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 | |
close() -> None
¶
Close the network renderer and release resources.
Source code in src/gsp_network/renderer/network_renderer.py
79 80 81 82 83 84 85 | |
get_canvas() -> Canvas
¶
Get the canvas associated with the network renderer.
Returns:
| Name | Type | Description |
|---|---|---|
Canvas |
gsp.core.canvas.Canvas
|
The canvas associated with the network renderer. |
Source code in src/gsp_network/renderer/network_renderer.py
71 72 73 74 75 76 77 | |
get_mpl_figure() -> matplotlib.figure.Figure
¶
Get the underlying Matplotlib figure.
Returns:
| Type | Description |
|---|---|
matplotlib.figure.Figure
|
matplotlib.figure.Figure: The Matplotlib figure used by the renderer. |
Source code in src/gsp_network/renderer/network_renderer.py
166 167 168 169 170 171 172 | |
get_remote_renderer_name() -> Literal['matplotlib', 'datoviz']
¶
Get the name of the remote renderer being used.
Returns:
| Type | Description |
|---|---|
typing.Literal['matplotlib', 'datoviz']
|
Literal["matplotlib", "datoviz"]: The name of the remote renderer. |
Source code in src/gsp_network/renderer/network_renderer.py
87 88 89 90 91 92 93 | |
render(viewports: Sequence[Viewport], visuals: Sequence[VisualBase], model_matrices: Sequence[TransBuf], cameras: Sequence[Camera]) -> bytes
¶
Render the scene remotely and update the matplotlib figure with the rendered image.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
viewports
|
typing.Sequence[gsp.core.viewport.Viewport]
|
The viewports to render. |
required |
visuals
|
typing.Sequence[gsp.types.visual_base.VisualBase]
|
The visuals to render. |
required |
model_matrices
|
typing.Sequence[gsp.types.transbuf.TransBuf]
|
The model matrices for the visuals. |
required |
cameras
|
typing.Sequence[gsp.core.camera.Camera]
|
The cameras to use for rendering. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
bytes |
bytes
|
The rendered image data in PNG format. |
Raises:
| Type | Description |
|---|---|
Exception
|
If the network request fails. |
Source code in src/gsp_network/renderer/network_renderer.py
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 | |
show() -> None
¶
Show the rendered canvas (blocking call).
Source code in src/gsp_network/renderer/network_renderer.py
157 158 159 160 161 162 163 164 | |
Tools Module¶
The tools module provides server utilities for network-based rendering.
gsp_network.tools
¶
tools package initialization.
Network Server¶
gsp_network.tools.network_server
¶
Server example using Flask to render a scene from JSON input.
- use Flask to create a simple web server
- render with matplotlib or datoviz based on environment variable
debug_save_payload: bool = False
module-attribute
¶
Enable saving of received payloads and rendered images for debugging.
debug_save_serial: int = 0
module-attribute
¶
Serial number for debug saved files.
ServerSample
¶
Sample class to demonstrate server functionality.
Source code in src/gsp_network/tools/network_server.py
123 124 125 126 127 128 129 130 131 132 | |
__init__()
¶
Initialize the server sample.
Source code in src/gsp_network/tools/network_server.py
126 127 128 | |
run()
¶
Run the Flask server.
Source code in src/gsp_network/tools/network_server.py
130 131 132 | |
render_scene_json() -> Response
¶
Flask route to render a scene from JSON input.
Returns:
| Name | Type | Description |
|---|---|---|
Response |
flask.Response
|
Flask response containing the rendered PNG image. |
Source code in src/gsp_network/tools/network_server.py
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 | |
text_cyan(text: str) -> str
¶
Return the given text string wrapped in ANSI escape codes for cyan color.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
The text to color. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
The colored text string. |
Source code in src/gsp_network/tools/network_server.py
36 37 38 39 40 41 42 43 44 45 | |
Network Server Kill¶
gsp_network.tools.network_server_kill
¶
Kill any process using port 5000 (commonly used for flask server).
in shell: lsof -ti tcp:5000 | xargs kill
main() -> int
¶
Main function to kill processes using port 5000.
Returns:
| Name | Type | Description |
|---|---|---|
int |
int
|
Exit code (0 for success, 1 for failure). |
Source code in src/gsp_network/tools/network_server_kill.py
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 | |
Viewport Events Network¶
gsp_network.viewport_events.viewport_events_network
¶
NetworkRenderer event handler for viewport events.
ViewportEventsNetwork
¶
Bases: gsp.types.viewport_events_base.ViewportEventsBase
NetworkRenderer event handler for viewport.
Code heavily inspired from ViewportEventsMatplotlib
Source code in src/gsp_network/viewport_events/viewport_events_network.py
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 | |
__init__(renderer: NetworkRenderer, viewport: Viewport) -> None
¶
Initialize the event handler with a renderer and viewport.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
renderer
|
gsp_network.renderer.NetworkRenderer
|
NetworkRenderer associated with this event handler. |
required |
viewport
|
gsp.core.Viewport
|
Viewport associated with this event handler. |
required |
Source code in src/gsp_network/viewport_events/viewport_events_network.py
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 | |
close()
¶
Close the event handler and release resources.
Source code in src/gsp_network/viewport_events/viewport_events_network.py
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 | |
Animator Network¶
gsp_network.animator.animator_network
¶
Network-based animator for GSP scenes.
Provides animation capabilities using a NetworkRenderer backend with support for real-time animation display and video export.
AnimatorNetwork
¶
Bases: gsp.types.animator_base.AnimatorBase
Animator for GSP scenes using a network renderer.
Manages animation loops with callback functions that update visuals each frame. Supports real-time display and video export in various formats.
Source code in src/gsp_network/animator/animator_network.py
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 | |
on_video_saved = Event[VideoSavedCalledback]()
instance-attribute
¶
Event triggered when the video has been successfully saved to disk.
__init__(network_renderer: NetworkRenderer, fps: int = 50, video_duration: float = 10.0, video_path: str | None = None, video_writer: str | None = None)
¶
Initialize the network animator.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
network_renderer
|
gsp_network.renderer.NetworkRenderer
|
The network renderer to use for rendering frames. |
required |
fps
|
int
|
Target frames per second for the animation. |
50
|
video_duration
|
float
|
Total duration of the animation in seconds. |
10.0
|
video_path
|
str | None
|
Path where the video should be saved. If None, no video is saved. |
None
|
video_writer
|
str | None
|
Video writer to use ("ffmpeg" or "pillow"). If None, auto-detected from extension. |
None
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If the video format is not supported. |
Source code in src/gsp_network/animator/animator_network.py
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 | |
add_callback(func: AnimatorFunc) -> None
¶
Add a callback to the animation loop.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
func
|
gsp.types.animator_types.AnimatorFunc
|
The animator function to call on each frame. |
required |
Source code in src/gsp_network/animator/animator_network.py
94 95 96 97 98 99 100 | |
event_listener(func: AnimatorFunc) -> AnimatorFunc
¶
Decorator to add a callback to the animation loop.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
func
|
gsp.types.animator_types.AnimatorFunc
|
The animator function to register as a callback. |
required |
Returns:
| Type | Description |
|---|---|
gsp.types.animator_types.AnimatorFunc
|
The wrapped animator function. |
Usage
@animation_loop.event_listener
def my_callback(delta_time: float) -> Sequence[VisualBase]:
...
# later, if needed
animation_loop.remove_callback(my_callback)
Source code in src/gsp_network/animator/animator_network.py
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 | |
remove_callback(func: AnimatorFunc) -> None
¶
Remove a callback from the animation loop.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
func
|
gsp.types.animator_types.AnimatorFunc
|
The animator function to remove. |
required |
Source code in src/gsp_network/animator/animator_network.py
102 103 104 105 106 107 108 | |
start(viewports: Sequence[Viewport], visuals: Sequence[VisualBase], model_matrices: Sequence[TransBuf], cameras: Sequence[Camera]) -> None
¶
Start the animation loop.
Begins rendering frames using registered callbacks to update visuals. In test mode (GSP_TEST=True), saves a single preview image instead of animating.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
viewports
|
typing.Sequence[gsp.core.viewport.Viewport]
|
Sequence of viewport regions to render into. |
required |
visuals
|
typing.Sequence[gsp.types.visual_base.VisualBase]
|
Sequence of visual elements to render and animate. |
required |
model_matrices
|
typing.Sequence[gsp.types.transbuf.TransBuf]
|
Sequence of model transformation matrices. |
required |
cameras
|
typing.Sequence[gsp.core.camera.Camera]
|
Sequence of cameras defining view and projection. |
required |
Source code in src/gsp_network/animator/animator_network.py
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 | |
stop()
¶
Stop the animation loop.
Stops the Matplotlib animation timer and clears internal state.
Source code in src/gsp_network/animator/animator_network.py
223 224 225 226 227 228 229 230 231 232 233 234 235 | |