Python

Python

NodeJS Python script wrapper and utility class.

Constructor

new Python(venvID, version, options)

Description:
  • Python constructor.

Source:
Example
const python = new Python();
await python.runPy("path/to/script.py", "path/to/cwd", ["arg1", "arg2"]);
Parameters:
Name Type Default Description
venvID string

The virtual environment ID.

version string 3.10.5-x64

The Python version.

options Object

The options for Python.

Extends

Methods

getConfig(key) → {Object}

Description:
  • Get the configuration.

Source:
Parameters:
Name Type Description
key string

The key of the configuration.

Returns:

The configuration.

Type
Object

getPackageInfo(pkg) → {Promise.<Object>}

Description:
  • Gets information about a specific Python package.

Source:
Parameters:
Name Type Description
pkg string

The name of the package to get information about.

Returns:

A promise that resolves to an object containing information about the package.

Type
Promise.<Object>

init()

Description:
  • Initialize Python class.

Source:

installPackage(pkg)

Description:
  • Installing a package

Source:
Example
await python.installPackage("litellm");
await python.installPackage("fastapi==0.109.0");
Parameters:
Name Type Description
pkg String

Package name

installPython() → {boolean}

Description:
  • Install Python.

Source:
Returns:

True if installation is successful, false otherwise.

Type
boolean

installRequirements(requirementsFile, force) → {Promise.<void>}

Description:
  • Installs the packages from a specific requirements file.

Source:
Parameters:
Name Type Description
requirementsFile string

The path to the requirements file.

force boolean

Whether to force the installation even if the requirements file is already installed.

Returns:

A promise that resolves when the packages have been installed.

Type
Promise.<void>

isPackage(packageName) → {Promise.<boolean>}

Description:
  • Checks if a specific Python package is installed.

Source:
Parameters:
Name Type Description
packageName string

The name of the package to check.

Returns:

A promise that resolves to true if the package is installed, false otherwise.

Type
Promise.<boolean>

isScript(scriptName) → {Promise.<boolean>}

Description:
  • Checks if a specific script exists.

Source:
Parameters:
Name Type Description
scriptName string

The name of the script to check.

Returns:

A promise that resolves to true if the script exists, false otherwise.

Type
Promise.<boolean>

listPackages() → {Promise.<Array>}

Description:
  • Lists all installed Python packages.

Source:
Returns:

A promise that resolves to an array of installed packages.

Type
Promise.<Array>

loadConfig() → {Object}

Description:
  • Load the configuration.

Source:
Returns:

The configuration.

Type
Object

off(evt, fn)

Description:
  • Removes an event
    Equal to $(document).off()

Source:
Since:
  • 4.3.20
Overrides:
Example
trans.off('transLoaded', (e, opt)=> {
	// do something
})
Parameters:
Name Type Description
evt String

Event name

fn function

Function to trigger

on(evt, fn)

Description:
  • Create a new event with JQuery eventing convenience
    Equal to $(document).on()

Source:
Since:
  • 4.3.20 trans.on('transLoaded', (e, opt)=> { // do something })
Overrides:
Parameters:
Name Type Description
evt String

Event name

fn function

Function to trigger

one(evt, fn)

Description:
  • Run the event once
    Trigger an event and immediately removes it
    Equal to $(document).one()

Source:
Since:
  • 4.3.20
Overrides:
Parameters:
Name Type Description
evt String

Event name

fn function

Function to trigger

processWith(processName, value) → {*}

Description:
  • Add a custom process to a value.

Source:
Overrides:
Parameters:
Name Type Description
processName String

Name of the process

value *

Value to be processed

Returns:
  • By default will return value as is
Type
*

rejectState(state, erroropt)

Description:
  • Reject a state

Source:
Overrides:
Parameters:
Name Type Attributes Description
state String

State name

error String <optional>

Error message

requirementIsInstalled(requirementsFile) → {boolean}

Description:
  • Checks if a specific requirements file is installed.

Source:
Parameters:
Name Type Description
requirementsFile string

The path to the requirements file.

Returns:

True if the requirements file is installed, false otherwise.

Type
boolean

resolveState(state, argopt)

Description:
  • Resolve a state

Source:
Overrides:
Parameters:
Name Type Attributes Description
state String

State name

arg * <optional>

Arguments to be passed to until

runPy(pathToPy, cwdopt, argsopt) → {String}

Description:
  • Run a python script and return the result

Source:
Parameters:
Name Type Attributes Description
pathToPy String

Path to the python script

cwd String <optional>

Directory where the python script will be called

args Array.<String> <optional>

Arguments to be passed to the script

Returns:

Output of the execution

Type
String

runPyDetached(pathToPy, cwdopt, argsopt) → {String}

Description:
  • Run a python script as a new shell window

Source:
Parameters:
Name Type Attributes Description
pathToPy String

Path to the python script

cwd String <optional>

Directory where the python script will be called

args Array.<String> <optional>

Arguments to be passed to the script

Returns:

Output of the execution

Type
String

saveConfig() → {Object}

Description:
  • Save the configuration.

Source:
Returns:

The configuration.

Type
Object

setConfig(key, value)

Description:
  • Set the configuration.

Source:
Parameters:
Name Type Description
key string

The key of the configuration.

value any

The value to set.

trigger(evt, fn)

Description:
  • Trigger an event
    Equal to $(document).trigger()

Source:
Since:
  • 4.3.20
Overrides:
Parameters:
Name Type Description
evt String

Event name

fn function

Function to trigger

uninstallPackage(pkg) → {Promise.<void>}

Description:
  • Uninstalls a specific Python package.

Source:
Parameters:
Name Type Description
pkg string | Array.<string>

The name of the package(s) to uninstall.

Returns:

A promise that resolves when the package(s) have been uninstalled.

Type
Promise.<void>

uninstallRequirements(requirementsFile) → {Promise.<void>}

Description:
  • Uninstalls the packages from a specific requirements file.

Source:
Parameters:
Name Type Description
requirementsFile string

The path to the requirements file.

Returns:

A promise that resolves when the packages have been uninstalled.

Type
Promise.<void>

(async) until(state) → {Promise.<*>}

Description:
  • State monitor. Wait until a state is resolved.
    Will wait until resolveState is called.
    Will immidiately executed if resolveState is already called

Source:
Since:
  • 4.12.4
Overrides:
Examples

Basic usage

await basicEventHandler.until("ready");

Asynchronous chaining

basicEventHandler.until("ready")
.then((result) = >{
	console.log(result);
})

basicEventHandler.resolveState("ready", "Hello world");
// prints: Hello world

Immidiately executed if `resolveState` with the same state has already been called

basicEventHandler.resolveState("ready", "App is ready");

var msg = await basicEventHandler.until("ready");
console.log(msg);
//immidately prints "App is ready"
Parameters:
Name Type Description
state String

State name

Returns:
  • Returns any variables passed on resolveState
Type
Promise.<*>

(static) downloader(from, to) → {Promise.<string>}

Description:
  • Downloads a file using Python.

Source:
Parameters:
Name Type Description
from string

The URL of the file to download.

to string

The path where to save the downloaded file.

Returns:

A promise that resolves to the path of the downloaded file.

Type
Promise.<string>

Type Definitions

Event

Description:
  • BasicEventHandler's event. This itself extends the standard Event API for html element. So, you can use the default element's event such as click, focus, blur, etc.
    Methods that uses Event are: on(), off(), one(), trigger()

Source:
Overrides:
See:

BasicEventHandler's event. This itself extends the standard Event API for html element. So, you can use the default element's event such as click, focus, blur, etc.
Methods that uses Event are: on(), off(), one(), trigger()

Type:
  • Event

State

Description:
  • Javascript's Promise type event handler.
    The state will switch from off to on once. And after that, it will stay in that state forever, so any further until() call will be resolved immidiately.
    Methods that uses Event are: until(), resolveState(), rejectState()

Source:
Since:
  • 4.12.1
Overrides:

Javascript's Promise type event handler.
The state will switch from off to on once. And after that, it will stay in that state forever, so any further until() call will be resolved immidiately.
Methods that uses Event are: until(), resolveState(), rejectState()

Type:
  • Promise