Skip to content

Commit 5987210

Browse files
Add readme and documentation
1 parent d528529 commit 5987210

2 files changed

Lines changed: 155 additions & 0 deletions

File tree

README.md

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
# Designer Python API
2+
3+
A TypeScript library and Vite plugin to interact with Designer Python APIs. This project provides tools to transform Python code into JavaScript modules with TypeScript definitions, enabling seamless integration of Designer Python modules into JavaScript/TypeScript projects.
4+
5+
## Features
6+
7+
- **Python API Client**: Provides a client to interact with Python APIs via HTTP.
8+
- **Python to JavaScript Transformation**: Converts Python functions into JavaScript modules.
9+
- **TypeScript Definitions**: Automatically generates TypeScript type definitions for Python functions.
10+
- **Vite Plugin**: Includes a Vite plugin for easy integration into Vite-based projects.
11+
12+
## Installation
13+
14+
Install the package using npm from GitHub:
15+
16+
```bash
17+
npm install disguise-one/designer-pythonapi
18+
```
19+
20+
## Usage
21+
22+
### Vite Plugin
23+
24+
To use the Vite plugin, add it to your Vite configuration, using the default options is recommended:
25+
26+
```ts
27+
// vite.config.ts
28+
import { designerPythonLoader } from '@disguise-one/designer-pythonapi/vite-loader';
29+
30+
export default {
31+
plugins: [
32+
designerPythonLoader(),
33+
],
34+
};
35+
```
36+
37+
#### Loader options
38+
39+
The Vite plugin accepts the following options:
40+
41+
- **`generateJavascript`** (optional): A boolean indicating whether to generate JavaScript files from the Python modules. If set to `true`, the loader will write the generated JavaScript code to a `.js` file alongside the Python module. Defaults to `false`.
42+
43+
- **`generateTypeDefinitions`** (optional): A boolean indicating whether to generate TypeScript type definition files (`.d.ts`) for the Python modules. If set to `true`, the loader will create type definitions based on the parsed Python functions. Defaults to `true`.
44+
45+
Example configuration:
46+
47+
```ts
48+
designerPythonLoader({
49+
generateJavascript: true,
50+
generateTypeDefinitions: true,
51+
});
52+
```
53+
54+
### Transforming Python Code
55+
56+
The plugin automatically transforms Python files (`.py`) into JavaScript modules during the build process. It uses the `__all__` variable to gather exported symbols from the file. Remember that Designer currently uses Python 2.7 syntax. For example, given the following Python code:
57+
58+
```python
59+
# myfile.py
60+
__all__ = ["venue_visibility"]
61+
62+
from d3 import resourceManager
63+
64+
def venue_visibility(show):
65+
cameraSettings = resourceManager.load('objects/stagerendersettings/visualiser.apx')
66+
cameraSettings.drawVenue = show
67+
return cameraSettings.drawVenue
68+
```
69+
70+
The plugin intercepts import operations and provides a JavaScript module with corresponding TypeScript definitions which allow seamless access to the exported python functions.
71+
72+
```ts
73+
import { ExecuteResponse } from '@disguise-one/designer-pythonapi'
74+
import { myfile } from './myfile.py'
75+
76+
// Import the python file into the target Designer instance (usually provided as `?director=<host:port>` to plugins)
77+
const { venue_visibility, registration } = myfile('director:port');
78+
79+
// Optionally check the result of registration - can be done asynchronously.
80+
const registrationResult: RegistrationResponse = await registration;
81+
if (registrationResult.status !== 0)
82+
throw Error("Failed to register 'myfile': " + registrationResult.message);
83+
84+
const result: ExecuteResponse = await venue_visibility(True);
85+
const returnValue = JSON.parse(result.returnValue);
86+
console.log(returnValue); // 3
87+
```
88+
89+
### Python API Client
90+
91+
The `PythonApiClient` class allows you to interact with Python APIs directly, without using import magic. Example usage:
92+
93+
```ts
94+
import { PythonApiClient } from '@disguise-one/designer-pythonapi';
95+
96+
const client = new PythonApiClient('localhost:8000', 'example', `
97+
__all__ = ["add"]
98+
99+
def add(a, b):
100+
return a + b
101+
`);
102+
103+
await client.register();
104+
const result = await client.executeScript('return add(1, 2)');
105+
console.log(result.returnValue); // Output: 3
106+
```
107+
108+
## Development
109+
110+
### Building the Project
111+
112+
To build the project, run:
113+
114+
```bash
115+
npm run build
116+
```
117+
118+
### Running Tests
119+
120+
To run the test suite, use:
121+
122+
```bash
123+
npm test
124+
```
125+
126+
### Debugging
127+
128+
The project includes debugging configurations for both Node.js and Python. Use the provided `.vscode/launch.json` configurations to debug the Vite plugin or the Python parser.
129+
130+
## File Structure
131+
132+
- **`src/`**: Contains the TypeScript source code.
133+
- `vite-loader.ts`: Vite plugin for transforming Python code.
134+
- `apiClient.ts`: Python API client implementation.
135+
- **`python_support/`**: Python scripts for parsing Python source code.
136+
- **`tests/`**: Test cases for the library.
137+
- **`dist/`**: Compiled output.
138+
139+
## License
140+
141+
This project is licensed under the MIT License.

src/vite-loader.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,21 @@ import { fileURLToPath } from 'url';
44
import * as fs from 'fs';
55

66
interface LoaderOptions {
7+
/**
8+
* Whether to generate JavaScript files from the Python modules.
9+
* If set to `true`, the loader will write the generated JavaScript code
10+
* to a `.js` file alongside the Python module.
11+
*
12+
* Default: `false`
13+
*/
714
generateJavascript?: boolean;
15+
16+
/**
17+
* Whether to generate TypeScript type definition files (`.d.ts`) for the Python modules.
18+
* If set to `true`, the loader will create type definitions based on the parsed Python functions.
19+
*
20+
* Default: `true`
21+
*/
822
generateTypeDefinitions?: boolean;
923
}
1024

0 commit comments

Comments
 (0)