Skip to content

Commit 0ba7cab

Browse files
committed
Update readme (typo and todo list)
1 parent aa31600 commit 0ba7cab

File tree

1 file changed

+25
-22
lines changed

1 file changed

+25
-22
lines changed

README.md

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,12 @@ Write Python endpoints in [SvelteKit](https://kit.svelte.dev/) and seamlessly de
1717
- [Fork of `sveltekit-modal`](#fork-of-sveltekit-modal)
1818
- [Possible future plans](#possible-future-plans)
1919

20-
2120
**This is very much in beta.**
2221

2322
## Current Features
2423

2524
- Write `+server.py` files nearly the same way you would write `+server.js` files
26-
- Deploy (quadi) automatically to Vercel Serverless
25+
- Deploy (quasi) automatically to Vercel Serverless
2726

2827
## Installing
2928

@@ -47,7 +46,7 @@ Write Python endpoints in [SvelteKit](https://kit.svelte.dev/) and seamlessly de
4746
- Update your `svelte.config.js`:
4847

4948
```javascript
50-
import adapter from "@sveltejs/adapter-vercel"; // Use the vercel adapter
49+
import adapter from "@sveltejs/adapter-vercel"; // Use the vercel adapter
5150
import { vitePreprocess } from "@sveltejs/kit/vite";
5251

5352
/** @type {import('@sveltejs/kit').Config} */
@@ -63,8 +62,10 @@ Write Python endpoints in [SvelteKit](https://kit.svelte.dev/) and seamlessly de
6362
```
6463

6564
- Update your `vercel.json`
65+
6666
- The build command prepares all your endpoints and copies them to the `/api` directory where Vercel looks for functions
6767
- Functions and Routes tell Vercel how to run and redirect function calls
68+
6869
```json
6970
{
7071
"buildCommand": "node ./node_modules/sveltekit-python-vercel/esm/src/vite/sveltekit_python_vercel/bin.mjs; vite build",
@@ -85,7 +86,8 @@ Write Python endpoints in [SvelteKit](https://kit.svelte.dev/) and seamlessly de
8586
- Write some `+server.py` endpoints. See the example section below.
8687

8788
## Testing Locally
88-
Using [Poetry](https://python-poetry.org/) to manage your virtual environments with this package is recommended.
89+
90+
Using [Poetry](https://python-poetry.org/) to manage your virtual environments with this package is recommended.
8991

9092
- Run `poetry init` to create a new virtual environment, and follow the steps. Or simply create a `pyproject.toml` like the one below.
9193

@@ -107,6 +109,7 @@ Using [Poetry](https://python-poetry.org/) to manage your virtual environments w
107109
requires = ["poetry-core"]
108110
build-backend = "poetry.core.masonry.api"
109111
```
112+
110113
- Required packages are python3.9 (that is what Vercel's runtime uses), `fastapi`, and `uvicorn`.
111114
- Install whatever other dependencies you need from pypi using `poetry add package-name`
112115

@@ -115,15 +118,16 @@ Using [Poetry](https://python-poetry.org/) to manage your virtual environments w
115118
- You should see both the usual SvelteKit server start as well as the unvicorn server (by default on `http://0.0.0.0:8000`) in the console.
116119

117120
## Deploying to Vercel
121+
118122
- At the moment this requires a tiny bit of extra labor besides just pushing to your repository. I believe this is because of the way Vercel looks for serverless functions, but I hope to make this a bit easier in the future.
119123

120124
- When you make changes to your python endpoints, you have to manually regenerate the `/api` folder by running:
121125
1. `poetry export -f requirements.txt --output requirements.txt --without-hashes`
122126
2. `node ./node_modules/sveltekit-python-vercel/esm/src/vite/sveltekit_python_vercel/bin.mjs`
123127
- Then commit `requirements.txt` and the changes in `/api` and push.
124128

125-
126129
Note:
130+
127131
- To make this a bit smoother, you can add a script to you `package.json`:
128132
```json
129133
"scripts": {
@@ -133,36 +137,34 @@ Note:
133137
```
134138
- and then just run `pnpm py-update`
135139

136-
137-
138-
139140
## Example
140141

141142
- Frontend: `/src/routes/py/+page.svelte`
143+
142144
```html
143145
<script lang="ts">
144146
let a = 0;
145147
let b = 0;
146148
let total = 0;
147149
148150
async function pyAddPost() {
149-
const response = await fetch('/py', {
150-
method: 'POST',
151+
const response = await fetch("/py", {
152+
method: "POST",
151153
body: JSON.stringify({ a, b }),
152154
headers: {
153-
'content-type': 'application/json'
154-
}
155+
"content-type": "application/json",
156+
},
155157
});
156158
let res = await response.json();
157159
total = res.sum;
158160
}
159161
160162
async function pyAddGet() {
161163
const response = await fetch(`/py?a=${a}&b=${b}`, {
162-
method: 'GET',
164+
method: "GET",
163165
headers: {
164-
'content-type': 'application/json'
165-
}
166+
"content-type": "application/json",
167+
},
166168
});
167169
168170
let res = await response.json();
@@ -174,24 +176,25 @@ Note:
174176

175177
<h3>POST Example</h3>
176178
<form>
177-
<input type="number" name="a" placeholder="Number 1" bind:value={a} />
178-
<input type="number" name="b" placeholder="Number 2" bind:value={b} />
179-
<button on:click|preventDefault={pyAddPost}>Add</button>
179+
<input type="number" name="a" placeholder="Number 1" bind:value="{a}" />
180+
<input type="number" name="b" placeholder="Number 2" bind:value="{b}" />
181+
<button on:click|preventDefault="{pyAddPost}">Add</button>
180182
</form>
181183
<h4>Total: {total}</h4>
182184

183185
<br />
184186

185187
<h3>GET Example</h3>
186188
<form>
187-
<input type="number" name="a" placeholder="Number 1" bind:value={a} />
188-
<input type="number" name="b" placeholder="Number 2" bind:value={b} />
189-
<button on:click|preventDefault={pyAddGet}>Add</button>
189+
<input type="number" name="a" placeholder="Number 1" bind:value="{a}" />
190+
<input type="number" name="b" placeholder="Number 2" bind:value="{b}" />
191+
<button on:click|preventDefault="{pyAddGet}">Add</button>
190192
</form>
191193
<h4>Total: {total}</h4>
192194
```
193195

194196
- Backend: `/src/routes/py/+server.py`
197+
195198
```python
196199
from pydantic import BaseModel
197200

@@ -213,6 +216,7 @@ Note:
213216
### Backend Caveats
214217

215218
There are currently a few things that have to be worked around.
219+
216220
- `GET` endpoints are directly fed the parameters from the url, so when you define an endpoint
217221
- All other endpoints are fed the body as a JSON. The recommended way to deal with this is to use a pydantic model and pass it as the singular input to the function.
218222

@@ -224,7 +228,6 @@ Check out the awesome [sveltekit-modal](https://github.com/semicognitive/sveltek
224228

225229
## Possible future plans
226230

227-
- [ ] Make API endpoints work with wildcard urls (e.g. `src/routes/user/[username]/+server.py) This currently throws an error during build.
228231
- [ ] Generate endpoints (/api folder) automatically during build
229232
- [ ] Auto create requirements.txt from pyproject.toml (both related to vercel functions being checked/handled before build)
230233
- [ ] Add form actions

0 commit comments

Comments
 (0)