Utils#
Utility functions for the ngapp module
- class ngapp.utils.BaseFrontend(link: LinkBase)#
Bases:
objectBase class for the frontend
- Parameters:
link (LinkBase)
- get_query_parameter(name: str)#
Get a query parameter from the URL
- Parameters:
name (str)
- class ngapp.utils.ComputeEnvironment(
- *,
- name: str = 'default',
- cpus: int = 1,
- memory: str = '1G',
- env_type: Literal['venv', 'docker', 'local'] = 'venv',
- dockerfile: str = '\nFROM python:3.12\nRUN python3 -m venv /venv\nENV PATH="/venv/bin:$PATH"\nRUN pip3 install watchdog websockets wheel pydantic==2.* urllib3 certifi pint colorama fore\nADD wheels /webapp_wheels\nRUN pip3 install /webapp_wheels/*.whl\nRUN unzip /webapp_wheels/ngapp.zip -d /venv/lib/python3.12/site-packages/\n',
Bases:
BaseModelDefines the maximal resource usage and type of compute environments on the backend
- Parameters:
name (str)
cpus (int)
memory (str)
env_type (Literal['venv', 'docker', 'local'])
dockerfile (str)
- class ngapp.utils.Environment(type: EnvironmentType, have_backend: bool, link=None)#
Bases:
objectEnvironment class to store the current environment
Following situations are possible:
- 1.) Pyodide environment
type = EnvironmentType.PYODIDE have_backend = True | False
This python instance is running in the browser in a web worker and communicates with the main thread via web worker postMessage interface.
- The backend might be:
No backend (frontend-only apps)
A full backend with http + websocket interface
- 2.) Local App environment
type = EnvironmentType.LOCAL_APP
This python instance is running on a local machine and communicates with the frontend (also running locally) via websockets.
- 3.) Compute environment
type = EnvironmentType.COMPUTE
This is a compute node running a compute function. It communicates with the full backend via http api interface.
- 4.) Standalone environment
type = EnvironmentType.STANDALONE
- Parameters:
type (EnvironmentType)
have_backend (bool)
- class ngapp.utils.EnvironmentType(*values)#
Bases:
str,EnumEnvironment type PYODIDE: Environment is a frontend (running with pyodide in the browser) COMPUTE: We are running a compute function on a separate compute node LOCAL_APP: Backend serves one app locally STANDALONE: Standalone mode with no frontend, used for testing
- class ngapp.utils.SettingsFile(path: Path)#
Bases:
objectJSON-backed key/value store for small settings files.
This is a generic helper that loads and saves a JSON file on demand and exposes
get,setandupdateutilities. It is used byUserSettingsfor both the mainconfig.jsonand any additional JSON settings files in the app’s settings directory.- Parameters:
path (Path)
- get(key: str, default=None)#
Get a setting by key, returning default if missing.
- Parameters:
key (str)
- set(key: str, value, *, autosave: bool = True) None#
Set a setting value and persist it (by default).
- Parameters:
key (str)
autosave (bool)
- Return type:
None
- update(key: str)#
Return a handler suitable for on_update_model_value.
Example:
nthreads = QInput( ui_label="Number of Threads", ui_model_value=app.usersettings.get("nthreads", default=None), ) nthreads.on_update_model_value( app.usersettings.update("nthreads") )
- Parameters:
key (str)
- class ngapp.utils.UserSettings(
- app_id: str,
- path: Path | None = None,
- app_name: str = 'ngapp',
Bases:
SettingsFileSimple persistent storage for user-wide, per-app settings.
Each app gets its own subfolder within the ngapp user config directory, and the main settings are stored in
config.jsoninside that folder.Additional JSON settings files for the same app can be created via
json_file().- Parameters:
app_id (str)
path (Path | None)
app_name (str)
- property directory: Path#
Directory that contains this app’s JSON settings files.
- json_file(name: str) SettingsFile#
Return a SettingsFile wrapper for an additional JSON file.
The file is created on first save.
.jsonis appended to name if it does not already end with it.- Parameters:
name (str)
- Return type:
- ngapp.utils.call_js(func, *args, **kwargs)#
Call a javascript function in the frontend
- ngapp.utils.copy_file(app)#
Create a new file and copy the data
- ngapp.utils.default_usersettings_dir(app_id: str, app_name: str = 'ngapp') Path#
Return the default directory for storing user-wide settings of an app.
The directory is placed inside the ngapp user config directory and named after the given app_id (sanitized to be filesystem-safe).
- Parameters:
app_id (str)
app_name (str)
- Return type:
Path
- ngapp.utils.default_usersettings_path(app_id: str, app_name: str = 'ngapp') Path#
Return the default config.json path for an app’s user settings.
- Parameters:
app_id (str)
app_name (str)
- Return type:
Path
- ngapp.utils.error(*args) None#
Log an message to the console
- Return type:
None
- ngapp.utils.get_job_component()#
Get the current job component of the running job
- ngapp.utils.is_production() bool#
Check if the code is executed in pyodide
- Return type:
bool
- ngapp.utils.is_pyodide() bool#
Check if the code is executed in pyodide
- Return type:
bool
- ngapp.utils.load_file_backend(file_id: str)#
Load a file from backend with given file id
- Parameters:
file_id (str)
- ngapp.utils.log(*args) None#
Log a message to the console
- Return type:
None
- ngapp.utils.new_file(app, data=None)#
Create a new file
- ngapp.utils.print_exception(
- ex,
- file=<_io.TextIOWrapper name='<stderr>' mode='w' encoding='utf-8'>,
Prints the exception and the traceback in red
- ngapp.utils.read_file(filename: str | Path) str#
Read a file from the filesystem
- Parameters:
filename (str | Path)
- Return type:
str
- ngapp.utils.read_file_binary(filename: str | Path) bytes#
Read a binary file from the filesystem
- Parameters:
filename (str | Path)
- Return type:
bytes
- ngapp.utils.read_json(filename: str | Path) dict#
Read a file from the filesystem
- Parameters:
filename (str | Path)
- Return type:
dict
- ngapp.utils.replace_app(new_app)#
Replace the current app instance with a new one
- ngapp.utils.set_directory(path: str)#
Context manager to change the current working directory
- Parameters:
path (str)
- ngapp.utils.temp_dir_with_files(
- data: dict[str, bytes],
- extract_zip: bool = False,
- return_list=True,
Context manager to handle files stored in a dictionary
- Parameters:
data (dict[str, bytes]) – The dictionary containing the file names and data
extract_zip (bool) – Whether to extract zip files
return_list – Whether to return a single file as list, or the file directly
- Yields:
A list containing the file paths objects
- Return type:
list[Path] | Path
- ngapp.utils.time_now() float#
Return the current time as a timestamp
- Return type:
float
- ngapp.utils.warning(*args) None#
Log a warning to the console
- Return type:
None
- ngapp.utils.write_file(
- filename: str | Path,
- data: str | bytes,
- binary: bool = False,
Write a file to the filesystem
- Parameters:
filename (str | Path)
data (str | bytes)
binary (bool)
- Return type:
int
- ngapp.utils.write_json(data: dict, filename: str | Path) None#
Write a file from the filesystem
- Parameters:
data (dict)
filename (str | Path)
- Return type:
None
- ngapp.utils.zip_directory(path: str, ignore: str = '*backend*') bytes#
Zip a directory and return the zip file as a bytes object
- Parameters:
path (str)
ignore (str)
- Return type:
bytes
- ngapp.utils.zip_modules(modules: list[str]) bytes#
Zip a list of python modules and return the zip file as a bytes object
- Parameters:
modules (list[str])
- Return type:
bytes