Skip to content

Commit eb15ace

Browse files
authored
Merge pull request #1 from SubstrateLabs/hwwu/toolchain-update
update the python toolchain to support both Rye and Poetry
2 parents d737c5c + b8d2033 commit eb15ace

File tree

17 files changed

+227
-846
lines changed

17 files changed

+227
-846
lines changed

_internal/_template/README.md

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
# Title
22

33
<details>
4-
<summary>How to run this example</summary>
4+
<summary>How to run this example</summary>
5+
<br/>
56

67
```bash
78
# Set your API key as an environment variable.
@@ -12,10 +13,16 @@ cd typescript # Navigate to the typescript example
1213
npm install # Install dependencies
1314
ts-node example.ts # Run the example
1415

15-
# Run the Python example
16-
# Note: First install dependencies in the root examples directory.
16+
# If using Poetry:
1717
cd python # Navigate to the python example
18-
poetry run python example.py # Run the example
18+
poetry install # Install dependencies and build the example
19+
poetry run main # Run the example
20+
21+
# If using Rye:
22+
# Update pyproject.toml to switch to Rye.
23+
cd python # Navigate to the python example
24+
rye sync # Install dependencies and build the example
25+
rye run main # Run the example
1926
```
2027

2128
</details>
@@ -24,13 +31,13 @@ We follow this procedure to create consistent, high volume content.
2431

2532
1. Come up with a short readable slug, e.g. `generate-json` and a title.
2633
2. Create a folder in the [examples repo](https://github.com/SubstrateLabs/examples), copying this folder
27-
3. Write the code in TS or Python, and keep it simple. Ideally it’s just a script with no additional dependencies.
28-
1. Consider creating illustrative variations of the script (e.g. `ComputeText` and `MultiComputeText` - [example](https://github.com/SubstrateLabs/examples/tree/main/basics/generate-text))
29-
2. Translate your script to the other language. (TODO: automated translation with Substrate)
30-
3. Make sure both examples run and produce simple polished output.
31-
4. Simplify the code
32-
1. Wrap lines (multi-line node declarations are easier to read)
33-
2. Consider inlining variables
34+
3. Write the code in TS or Python, and keep it simple. Ideally it’s just a script with no additional dependencies.
35+
1. Consider creating illustrative variations of the script (e.g. `ComputeText` and `MultiComputeText` - [example](https://github.com/SubstrateLabs/examples/tree/main/basics/generate-text))
36+
2. Translate your script to the other language. (TODO: automated translation with Substrate)
37+
3. Make sure both examples run and produce simple polished output.
38+
4. Simplify the code
39+
1. Wrap lines (multi-line node declarations are easier to read)
40+
2. Consider inlining variables
3441
4. Fill out this README with walkthrough text and generate new image assets.
3542

3643
![hero](hero.png)
@@ -82,7 +89,6 @@ If your example is a graph, create a diagram.
8289

8390
![diagram](diagram.svg)
8491

85-
8692
To edit the diagram, run:
8793

8894
```bash

_internal/_template/python/.gitignore

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# python generated files
2+
__pycache__/
3+
*.py[oc]
4+
build/
5+
dist/
6+
wheels/
7+
*.egg-info
8+
9+
# venv
10+
.venv

_internal/_template/python/README.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Substrate Example Templates in Python
2+
3+
This is a Substrate example template written in Python. To run this example,
4+
5+
```bash
6+
# Set your API key as an environment variable.
7+
# Get one here https://www.substrate.run/dashboard/keys if this is your first time.
8+
export SUBSTRATE_API_KEY=<your Substrate API key>
9+
10+
# Navigate to the python example directory.
11+
cd python
12+
```
13+
14+
To run the example with Poetry (default), run the following.
15+
16+
```bash
17+
poetry install
18+
poetry run main
19+
```
20+
21+
To run the example with Rye, comment out the Poetry sections and uncomment the Rye sections in `pyproject.toml` and run the following.
22+
23+
```bash
24+
rye sync
25+
rye run main
26+
```
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[virtualenvs]
2+
in-project = true
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
[project]
2+
name = "template"
3+
version = "0.1.0"
4+
description = ""
5+
authors = [{ name = "Hanwen Wu", email = "[email protected]" }]
6+
readme = "README.md"
7+
8+
requires-python = ">= 3.9"
9+
dependencies = ["substrate"]
10+
11+
[project.scripts]
12+
main = "python.example:main"
13+
14+
[tool.ruff]
15+
line-length = 120
16+
output-format = "grouped"
17+
target-version = "py310"
18+
19+
[tool.pyright]
20+
venvPath = "."
21+
venv = ".venv"
22+
23+
##############################
24+
# Uncomment for Poetry support
25+
##############################
26+
27+
[build-system]
28+
requires = ["poetry-core"]
29+
build-backend = "poetry.core.masonry.api"
30+
31+
[tool.poetry]
32+
name = "template"
33+
version = "0.1.0"
34+
description = ""
35+
authors = ["Hanwen Wu <[email protected]>"]
36+
readme = "README.md"
37+
38+
packages = [{ include = "python", from = "src" }]
39+
40+
[tool.poetry.dependencies]
41+
python = "^3.9"
42+
substrate = "220240617.*"
43+
44+
[tool.poetry.group.dev.dependencies]
45+
ruff = "^0.6.1"
46+
marimo = "^0.8.0"
47+
48+
[tool.poetry.scripts]
49+
main = "python.example:main"
50+
51+
52+
###########################
53+
# Uncomment for Rye support
54+
###########################
55+
56+
# [build-system]
57+
# requires = ["hatchling"]
58+
# build-backend = "hatchling.build"
59+
60+
# [tool.rye]
61+
# managed = true
62+
# dev-dependencies = ["ruff>=0.6.1", "marimo>=0.8.0"]
63+
64+
# [tool.hatch.metadata]
65+
# allow-direct-references = true
66+
67+
# [tool.hatch.build.targets.wheel]
68+
# packages = ["src/python"]

_internal/_template/python/src/python/__init__.py

Whitespace-only changes.

basics/generate-text/README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,10 @@ ts-node example-multi.ts # Run the MultiComputeText example
1515

1616
# Run the Python example
1717
# Note: First install dependencies in the root examples directory.
18-
cd python # Navigate to the python example
19-
poetry run python example.py # Run the ComputeText example
20-
poetry run python example-multi.py # Run the MultiComputeText example
18+
cd python # Navigate to the python example
19+
poetry install # Install dependencies and build the example
20+
poetry run example # Run the ComputeText example
21+
poetry run example-multi # Run the MultiComputeText example
2122
```
2223

2324
</details>

basics/generate-text/python/README.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Substrate GenerateText Examples
2+
3+
To run this example,
4+
5+
```bash
6+
# Set your API key as an environment variable.
7+
# Get one here https://www.substrate.run/dashboard/keys if this is your first time.
8+
export SUBSTRATE_API_KEY=<your Substrate API key>
9+
10+
# Navigate to the python example directory.
11+
cd python
12+
```
13+
14+
To run the example with Poetry (default),
15+
16+
```bash
17+
poetry install
18+
poetry run example
19+
poetry run example-multi
20+
```
21+
22+
To run the example with Rye, comment out the Poetry sections and uncomment the Rye sections in `pyproject.toml` and run the following.
23+
24+
```bash
25+
rye sync
26+
rye run example
27+
rye run example-multi
28+
```
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[virtualenvs]
2+
in-project = true

0 commit comments

Comments
 (0)