Utils#

Utility functions for the ngapp module

class ngapp.utils.BaseFrontend#

Bases: object

Base class for the frontend

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: BaseModel

Defines 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)#

Bases: object

Environment 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

  • A “full” backend with http api interface + 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:
class ngapp.utils.EnvironmentType(*values)#

Bases: str, Enum

Environment type PYODIDE: Environment is a frontend (running with pyodide in the browser) COMPUTE: We are running a compute function on a separate compute node APP: Backend serves one app locally STANDALONE: Standalone mode with no frontend, used for testing

ngapp.utils.confirm(title='', message='', on_ok=None, on_cancel=None)#

Show a confirmation dialog

ngapp.utils.copy_simulation(data: dict)#

Create a new simulation and copy the data

Parameters:

data (dict)

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_simulation(file_id: str)#

Load a simulation from file id

Parameters:

file_id (str)

ngapp.utils.log(*args) None#

Log a message to the console

Return type:

None

ngapp.utils.new_simulation()#

Create a new simulation

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.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,
) list[Path] | Path#

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,
) int#

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) bytes#

Zip a list of python modules and return the zip file as a bytes object

Return type:

bytes