Next.js Discord

Discord Forum

Running a Python script on NextJS 13 (serverless) backend

Unanswered
Abyssinian posted this in #help-forum
Open in Discord
AbyssinianOP
I have a small NextJS webapp running NextJS 13, hosted on Vercel. I made a simple serverless function (in TS) to receive a PDF, do some processing, and return a JSON. Part of this involves passing the PDF to a python script (in python-util.py) for some additional processing. My file structure is as follows:

- src/app/ -- api/ ---- parse-pdf/ ------ route.ts ------ python-util.py -- page.tsx

python-util.pywill return a JSON string to route.ts. What is the best practice for doing something like this, i.e mixing Python and Javascript/Typescript?
1. Should I use Node's child_process method in route.ts to launch the python-util.py script as subprocess?
2. Should I instead make the python code its own endpoint, like on deploy it separately on Vercel? If so, where in my file structure do I do this?
3. Just give up and rewrite all the Python in JS
4. Something I'm not thinking of?

Thank you

5 Replies

European sprat
Another option would be to deploy a python API endpoint you make requests to from the nextjs API. With serverless deployments you aren't able to run other commands like python
Another thing that's also somewhat overkill and you lose all the Vercel serverless/edge stuff is running a standalone nextjs app so you can run whatever commands you want
AbyssinianOP
@European sprat Can you run python in a standalone NextJS app?
European sprat
A standalone build of nextjs outputs all the files you need and you run it with node server.js like any other nodejs app. Wherever that gets deployed, you could possibly have python or other executables you can use
All depends on the host