Integrating a Python endpoint in the t3 stack

Is it possible to integrate a Python endpoint in the t3 stack? I need a Python endpoint because the package that I wanna use is an AI one and currently unavailable in JS/TS.
18 Replies
Tom
Tom2y ago
couldnt you just run it as a separate server and then call it from within a trpc endpoint? not the most efficent but itll get the job done if you want to leverage trpc
cje
cje2y ago
Write a GraphQL or OpenAPI Schema for it and make a request to it from your tRPC backend
jdsl
jdsl2y ago
This might be what you are looking for if using Vercel for hosting: https://vercel.com/docs/concepts/functions/serverless-functions/runtimes/python
Vercel Documentation
Using the Python Runtime with Serverless Functions
Learn how to use the Python runtime to compile Python Serverless Functions on Vercel.
shikishikichangchang
Yes, I can host a separate api for that endpoint but ideally I would like to keep everything in the same monorepo. Is it possible? Has anyone done it? I read online that some people got it working by putting the *.py files in /api (not /pages/api) I tried and couldnt get it working
import type { ChildProcessWithoutNullStreams } from "child_process";
import { spawn } from "child_process";
import { trycatch } from "../../../utils/trycatch";
import { createTRPCRouter, protectedProcedure } from "../trpc";

export const pythonRouter = createTRPCRouter({
test: protectedProcedure.query(async () => {
return await trycatch({
fn: () => {
const python: ChildProcessWithoutNullStreams = spawn("python3", [
"src/server/api/python/testing.py",
]);
return new Promise<string>((resolve, reject) => {
let output = "";

python.stdout.on("data", (data: Buffer) => {
output += data.toString();
});

python.stderr.on("data", (data: Buffer) => {
reject(data.toString());
});

python.on("close", (code) => {
if (code && code !== 0) {
reject(`Process exited with code ${code}`);
} else {
resolve(output);
}
});
});
},
errorMessages: ["Failed to run python code"],
})();
}),
});
import type { ChildProcessWithoutNullStreams } from "child_process";
import { spawn } from "child_process";
import { trycatch } from "../../../utils/trycatch";
import { createTRPCRouter, protectedProcedure } from "../trpc";

export const pythonRouter = createTRPCRouter({
test: protectedProcedure.query(async () => {
return await trycatch({
fn: () => {
const python: ChildProcessWithoutNullStreams = spawn("python3", [
"src/server/api/python/testing.py",
]);
return new Promise<string>((resolve, reject) => {
let output = "";

python.stdout.on("data", (data: Buffer) => {
output += data.toString();
});

python.stderr.on("data", (data: Buffer) => {
reject(data.toString());
});

python.on("close", (code) => {
if (code && code !== 0) {
reject(`Process exited with code ${code}`);
} else {
resolve(output);
}
});
});
},
errorMessages: ["Failed to run python code"],
})();
}),
});
I was able to get this code to run locally It basically calls a python file located next to itself but it fails to run on vercel I suspect the reason is that vercel doesnt have python installed I was wondering if it's possble to install all nextjs dependencies and pythons?
Patrick
Patrick2y ago
Vercel Documentation
Using the Python Runtime with Serverless Functions
Learn how to use the Python runtime to compile Python Serverless Functions on Vercel.
shikishikichangchang
I did
Patrick
Patrick2y ago
The page is answering your question: Yes its possible, yes you can install packages (see bottom of the page requirements.txt)
shikishikichangchang
Im asking if it's possible to install both nextJS and python together under pages/api
Patrick
Patrick2y ago
Yes, thats possible
Patrick
Patrick2y ago
Ah, maybe not. In their example repo it looks like this: https://github.com/vercel/examples/tree/main/python/hello-world
GitHub
examples/python/hello-world at main · vercel/examples
Enjoy our curated collection of examples and solutions. Use these patterns to build your own robust and scalable applications. - examples/python/hello-world at main · vercel/examples
Patrick
Patrick2y ago
So you might need another deployment.
Patrick
Patrick2y ago
Stack Overflow
Python Serverless Function Vercel - Next.js
I found out that I could use Python to create a serverless function inside a Next.js project. Once deployed to Vercel, it will get converted into a serverless function. I went through the docs and ...
Patrick
Patrick2y ago
In short, if you're using another language to write a serverless function inside a Next.js project. Be sure to place it inside an api folder that sits in the root directory of the project (if there's none, create one).
In short, if you're using another language to write a serverless function inside a Next.js project. Be sure to place it inside an api folder that sits in the root directory of the project (if there's none, create one).
shikishikichangchang
That's what I did and failed haha .
jdsl
jdsl2y ago
Deployment failed or getting error hitting the endpoint? I'll test on one of my projects to see if I can get it to work Found some more info here: https://github.com/vercel/vercel/discussions/6197 https://github.com/vercel/vercel/discussions/4023 Looks like while it is possible to mix & match, it's not as straight forward. You have to move the api files to the root of your project /api and it might cause a few issues with 3rd party packages that are expecting pages/api to exist.
Want results from more Discord servers?
Add your server