Skip to content

Commit 28ff962

Browse files
prabhakk-mwkrisctl
authored andcommitted
Adds MWI_SESSION_NAME environment variable for custom browser tab titles.
1 parent dac9bbf commit 28ff962

File tree

11 files changed

+96
-14
lines changed

11 files changed

+96
-14
lines changed

Advanced-Usage.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ The following table describes all the environment variables that you can set to
3333
| **MWI_PROCESS_START_TIMEOUT** | integer (optional) | `1234` | This field controls the time (in seconds) for which `matlab-proxy` waits for the processes it spins up, viz: MATLAB & Xvfb, to respond. By default, this value is `600 seconds`. A timeout could either indicate an issue with the spawned processes or be a symptom of a resource-constrained environment. Increase this value if your environment needs more time for the spawned processes to start.|
3434
| **MWI_MATLAB_STARTUP_SCRIPT** | string (optional) | `"addpath('/path/to/a/folder'), c=12"` | Executes string provided at MATLAB startup. For details, see [Run Custom MATLAB Startup Code](#run-custom-matlab-startup-code) |
3535
| **MWI_SHUTDOWN_ON_IDLE_TIMEOUT** | integer (optional) | 60 | Defines the duration in minutes, that `matlab-proxy` remains idle before shutting down. When you do not set the variable, `matlab-proxy` will not shut down when idle. For details, [see Shutdown on Idle](#shutdown-on-idle). |
36+
| **MWI_SESSION_NAME** | string (optional) | "My MATLAB" | Specifies the title of the browser tab, displayed as `{MWI_SESSION_NAME} - MATLAB R20xxy`. Default value is `MATLAB R20XXy`. |
3637

3738
## Shutdown on Idle
3839

gui/src/components/App/index.jsx

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ import {
4040
selectMatlabStarting,
4141
selectIsIdleTimeoutEnabled,
4242
selectMatlabStopping,
43+
selectBrowserTitle,
4344
selectIntegrationName
4445
} from '../../selectors';
4546

@@ -54,7 +55,7 @@ import blurredBackground from './MATLAB-env-blur.png';
5455
import EntitlementSelector from '../EntitlementSelector';
5556
import { BUFFER_TIMEOUT_DURATION, MWI_AUTH_TOKEN_NAME_FOR_HTTP } from '../../constants';
5657

57-
function App () {
58+
function App() {
5859
const dispatch = useDispatch();
5960

6061
const overlayVisible = useSelector(selectOverlayVisible);
@@ -76,6 +77,7 @@ function App () {
7677
const isConcurrencyEnabled = useSelector(selectIsConcurrencyEnabled);
7778
const wasEverActive = useSelector(selectWasEverActive);
7879
const integrationName = useSelector(selectIntegrationName);
80+
const browserTitle = useSelector(selectBrowserTitle);
7981

8082
// Timeout duration is specified in seconds, but useTimeoutFn accepts timeout values in ms.
8183
const idleTimeoutDurationInMS = useSelector(selectIdleTimeoutDurationInMS);
@@ -93,7 +95,7 @@ function App () {
9395

9496

9597
// callback that will fire once the IDLE timer expires
96-
function terminationFn () {
98+
function terminationFn() {
9799
// Reset the timer if MATLAB is either starting or stopping or is busy
98100
if (isMatlabStarting || isMatlabStopping || isMatlabBusy) {
99101
idleTimerReset();
@@ -303,7 +305,7 @@ function App () {
303305
if (hasFetchedEnvConfig) {
304306
const queryParams = parseQueryParams(window.location);
305307
const token = queryParams.get(MWI_AUTH_TOKEN_NAME_FOR_HTTP);
306-
308+
document.title = `${browserTitle}`;
307309
if (token) {
308310
dispatch(updateAuthStatus(token));
309311
}
@@ -331,7 +333,7 @@ function App () {
331333
overlayContent = <ShutdownWarning
332334
bufferTimeout={BUFFER_TIMEOUT_DURATION}
333335
resumeCallback={() => {
334-
// Restart IDLE timer
336+
// Restart IDLE timer
335337
idleTimerReset();
336338
setIdleTimerHasExpired(false);
337339

@@ -386,7 +388,7 @@ function App () {
386388
if (matlabUp) {
387389
matlabJsd = (!authEnabled || isAuthenticated)
388390
? (<MatlabJsd handleUserInteraction={handleUserInteraction} url={matlabUrl} iFrameRef={MatlabJsdIframeRef} shouldListenForEvents={shouldListenForEvents} />)
389-
: <img style={{ objectFit: 'fill' }}src={blurredBackground} alt='Blurred MATLAB environment'/>;
391+
: <img style={{ objectFit: 'fill' }} src={blurredBackground} alt='Blurred MATLAB environment' />;
390392
}
391393

392394
const overlayTrigger = overlayVisible

gui/src/selectors/index.js

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -260,9 +260,23 @@ export const selectIsIdleTimeoutEnabled = createSelector(
260260
export const selectIdleTimeoutDurationInMS = createSelector(
261261
selectIsIdleTimeoutEnabled,
262262
selectIdleTimeoutDuration,
263-
(isTimeoutEnabled, idleTimeoutDuration) => { return isTimeoutEnabled
264-
? idleTimeoutDuration * 1000
265-
: undefined; }
263+
(isTimeoutEnabled, idleTimeoutDuration) => {
264+
return isTimeoutEnabled
265+
? idleTimeoutDuration * 1000
266+
: undefined;
267+
}
268+
);
269+
270+
export const selectBrowserTitle = createSelector(
271+
selectHasFetchedEnvConfig,
272+
selectEnvConfig,
273+
(hasFetchedEnvConfig, envConfig) => {
274+
if (hasFetchedEnvConfig) {
275+
return envConfig.browserTitle;
276+
} else {
277+
return 'MATLAB';
278+
}
279+
}
266280
);
267281

268282
export const selectIntegrationName = createSelector(

matlab_proxy/app.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,8 @@ async def get_env_config(req):
209209
"supportedVersions": constants.SUPPORTED_MATLAB_VERSIONS,
210210
}
211211

212+
config["browserTitle"] = state.settings["browser_title"]
213+
212214
# Send timeout duration for the idle timer as part of the response
213215
config["idleTimeoutDuration"] = state.settings["mwi_idle_timeout"]
214216

matlab_proxy/app_state.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -896,8 +896,8 @@ def create_server_info_file(self):
896896

897897
mwi_server_info_file = mwi_logs_dir / "mwi_server.info"
898898
mwi_auth_token_str = token_auth.get_mwi_auth_token_access_str(self.settings)
899-
with open(mwi_server_info_file, "w") as fh:
900-
fh.write(self.settings["mwi_server_url"] + mwi_auth_token_str + "\n")
899+
with open(mwi_server_info_file, "w", encoding="utf-8") as fh:
900+
fh.write(self.settings["mwi_server_url"] + mwi_auth_token_str + "\n" + self.settings["browser_title"] + "\n")
901901
self.mwi_server_session_files["mwi_server_info_file"] = mwi_server_info_file
902902
logger.debug(f"Server info stored into: {mwi_server_info_file}")
903903

matlab_proxy/settings.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
from matlab_proxy.util import mwi, system
2222
from matlab_proxy.util.cookie_jar import HttpOnlyCookieJar
2323
from matlab_proxy.util.mwi import environment_variables as mwi_env
24-
from matlab_proxy.util.mwi import token_auth
24+
from matlab_proxy.util.mwi import token_auth, session_name
2525
from matlab_proxy.util.mwi.exceptions import (
2626
FatalError,
2727
MatlabInstallError,
@@ -222,6 +222,7 @@ def get_dev_settings(config):
222222
"is_windowmanager_available": False,
223223
"mwi_idle_timeout": None,
224224
"cookie_jar": _get_cookie_jar(),
225+
"browser_title": session_name.get_browser_title("R2020b"),
225226
}
226227

227228

@@ -324,7 +325,6 @@ def get_server_settings(config_name):
324325
)
325326

326327
cookie_jar = _get_cookie_jar()
327-
328328
return {
329329
"create_xvfb_cmd": create_xvfb_cmd,
330330
"base_url": mwi.validators.validate_base_url(
@@ -406,6 +406,7 @@ def get_matlab_settings():
406406
**mw_licensing_urls,
407407
"nlm_conn_str": nlm_conn_str,
408408
"has_custom_code_to_execute": has_custom_code_to_execute,
409+
"browser_title": session_name.get_browser_title(matlab_version),
409410
}
410411

411412

matlab_proxy/util/list_servers.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ def print_server_info():
3131
for server in search_results:
3232
server_number += 1
3333
with open(server) as f:
34-
server_info = f.read()
35-
print_output += str(server_number) + ". " + str(server_info)
34+
server_info = f.readline().strip()
35+
print_output += str(server_number) + ". " + str(server_info) + "\n"
3636

3737
print_output += str(
3838
mwi_util.prettify(

matlab_proxy/util/mwi/environment_variables.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,11 @@ def get_env_name_shutdown_on_idle_timeout():
172172
return "MWI_SHUTDOWN_ON_IDLE_TIMEOUT"
173173

174174

175+
def get_env_name_session_name():
176+
"""User specified session name for the MATLAB Proxy instance, used to set the browser title."""
177+
return "MWI_SESSION_NAME"
178+
179+
175180
class Experimental:
176181
"""This class houses functions which are undocumented APIs and Environment variables.
177182
Note: Never add any state to this class. Its only intended for use as an abstraction layer
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Copyright 2025 The MathWorks, Inc.
2+
"""This file provides functions to set a session name for the MATLAB Proxy instance."""
3+
4+
import os
5+
from matlab_proxy.util.mwi import environment_variables as mwi_env
6+
from matlab_proxy.util.mwi import logger as mwi_logger
7+
8+
logger = mwi_logger.get()
9+
10+
def _get_session_name():
11+
"""Get the session name for the MATLAB Proxy instance.
12+
13+
Returns:
14+
str: returns the user-defined session name if set, otherwise returns None.
15+
"""
16+
return os.getenv(mwi_env.get_env_name_session_name(), None)
17+
18+
19+
def get_browser_title(matlab_version) -> str:
20+
"""Get the browser title for the MATLAB Proxy instance."""
21+
22+
browser_title = "MATLAB " + (matlab_version or "")
23+
session_name = _get_session_name()
24+
if session_name:
25+
browser_title = session_name + " - " + browser_title
26+
logger.info("Session Name set to : %s", session_name)
27+
return browser_title

tests/unit/test_app.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,7 @@ async def test_get_env_config(test_server):
360360
"should_show_shutdown_button": True,
361361
"isConcurrencyEnabled": "foobar",
362362
"idleTimeoutDuration": 100,
363+
"browserTitle": "MATLAB R2020b",
363364
}
364365
resp = await test_server.get("/get_env_config")
365366
assert resp.status == HTTPStatus.OK

0 commit comments

Comments
 (0)