Skip to content

Commit 84f0137

Browse files
committed
feat: add example setup using virtual env
1 parent 9ca6eeb commit 84f0137

File tree

2 files changed

+143
-1
lines changed

2 files changed

+143
-1
lines changed

examples/README.md

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,54 @@ This example project demonstrates how to use the Nutrient DWS Python Client for
1313

1414
## Prerequisites
1515

16-
- Python 3.8 or higher
16+
- Python 3.10 or higher
1717
- pip
1818

1919
## Setup
2020

21+
### Option 1: Virtual Environment Setup
22+
23+
1. Clone the repository:
24+
```bash
25+
git clone https://github.com/pspdfkit-labs/nutrient-dws-client-python.git
26+
cd nutrient-dws-client-python
27+
```
28+
29+
2. Build the package from source:
30+
```bash
31+
python -m build
32+
```
33+
34+
3. Navigate to the examples directory:
35+
```bash
36+
cd examples
37+
```
38+
39+
4. Set up and activate the virtual environment:
40+
```bash
41+
# Set up the virtual environment and install dependencies
42+
python setup_venv.py
43+
44+
# Activate the virtual environment
45+
# On macOS/Linux:
46+
source example_venv/bin/activate
47+
48+
# On Windows:
49+
example_venv\Scripts\activate
50+
```
51+
52+
5. Create a `.env` file from the example:
53+
```bash
54+
cp .env.example .env
55+
```
56+
57+
6. Edit the `.env` file and add your Nutrient DWS Processor API key. You can sign up for a free API key by visiting [Nutrient](https://www.nutrient.io/api/):
58+
```
59+
NUTRIENT_API_KEY=your_api_key_here
60+
```
61+
62+
### Option 2: Development Mode Setup
63+
2164
1. Clone the repository:
2265
```bash
2366
git clone https://github.com/pspdfkit-labs/nutrient-dws-client-python.git

examples/setup_venv.py

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
#!/usr/bin/env python3
2+
"""
3+
Setup script for creating and configuring the examples virtual environment.
4+
This script creates a virtual environment and installs the nutrient-dws package
5+
from the built distribution files.
6+
"""
7+
8+
import os
9+
import subprocess
10+
import sys
11+
from pathlib import Path
12+
13+
def run_command(cmd, cwd=None, check=True):
14+
"""Run a command and return the result."""
15+
print(f"Running: {' '.join(cmd) if isinstance(cmd, list) else cmd}")
16+
try:
17+
result = subprocess.run(
18+
cmd,
19+
shell=isinstance(cmd, str),
20+
cwd=cwd,
21+
check=check,
22+
capture_output=True,
23+
text=True
24+
)
25+
if result.stdout:
26+
print(result.stdout)
27+
return result
28+
except subprocess.CalledProcessError as e:
29+
print(f"Error: {e}")
30+
if e.stderr:
31+
print(f"Error output: {e.stderr}")
32+
raise
33+
34+
def main():
35+
# Get the current directory (examples folder)
36+
examples_dir = Path(__file__).parent
37+
project_root = examples_dir.parent
38+
dist_dir = project_root / "dist"
39+
40+
print(f"Setting up virtual environment in: {examples_dir}")
41+
42+
# Create virtual environment
43+
venv_path = examples_dir / "example_venv"
44+
if venv_path.exists():
45+
print("Virtual environment already exists. Removing...")
46+
import shutil
47+
shutil.rmtree(venv_path)
48+
49+
print("Creating virtual environment...")
50+
run_command([sys.executable, "-m", "venv", "example_venv"], cwd=examples_dir)
51+
52+
# Determine the python executable in the venv
53+
if sys.platform == "win32":
54+
python_exe = venv_path / "Scripts" / "python.exe"
55+
pip_exe = venv_path / "Scripts" / "pip.exe"
56+
else:
57+
python_exe = venv_path / "bin" / "python"
58+
pip_exe = venv_path / "bin" / "pip"
59+
60+
# Upgrade pip
61+
print("Upgrading pip...")
62+
run_command([str(pip_exe), "install", "--upgrade", "pip"])
63+
64+
# Install the wheel and tar.gz files
65+
wheel_file = dist_dir / "nutrient_dws-2.0.0-py3-none-any.whl"
66+
tar_file = dist_dir / "nutrient_dws-2.0.0.tar.gz"
67+
68+
if wheel_file.exists():
69+
print("Installing nutrient-dws from wheel...")
70+
run_command([str(pip_exe), "install", str(wheel_file)])
71+
elif tar_file.exists():
72+
print("Installing nutrient-dws from tar.gz...")
73+
run_command([str(pip_exe), "install", str(tar_file)])
74+
else:
75+
print("Error: Neither wheel nor tar.gz file found in dist directory")
76+
print("Please build the package first using: python -m build")
77+
sys.exit(1)
78+
79+
# Install example requirements
80+
requirements_file = examples_dir / "requirements.txt"
81+
if requirements_file.exists():
82+
print("Installing example requirements...")
83+
run_command([str(pip_exe), "install", "-r", str(requirements_file)])
84+
85+
print("\n" + "="*50)
86+
print("Virtual environment setup complete!")
87+
print(f"Virtual environment location: {venv_path}")
88+
print("\nTo activate the virtual environment:")
89+
if sys.platform == "win32":
90+
print(f" {venv_path / 'Scripts' / 'activate.bat'}")
91+
else:
92+
print(f" source {venv_path / 'bin' / 'activate'}")
93+
94+
print("\nTo run examples:")
95+
print(" python src/direct_method.py")
96+
print(" python src/workflow.py")
97+
98+
if __name__ == "__main__":
99+
main()

0 commit comments

Comments
 (0)