Nextjs and python
Answered
In&Out posted this in #help-forum
In&OutOP
Hey folks, so what i need to do is i want to make a site with nextjs and then use python, but my only use for python is to fetch and process some data that can be only done in python, so instead of making whole backend with python, can i just run a python script inside nextjs then show the data on frontend? I also need live updates if possible
54 Replies
I've tried something like that https://github.com/lbke/snext
Vercel CLI allows to run vercel supported runtimes and can replace next CLI
so you can have Python API routes
don't know if it works with route handlers though
Then you can also run Python script from your JS code which is probably much simpler
using child_process for instance
you just have to make sure your host can run python
Finally there are ways to bundle built code in Node.js but that's tricky, I have no experience of that
@Eric Burel I've tried something like that https://github.com/lbke/snext
In&OutOP
Hmm okay, i dont really need route with python, i just wanted a python file data to be accessed from javascript
Example, like this, oversimplified
def hi():
return "hi"
export hi from ./hi
export const func(){
return <div>{hi.something}</div>
Is this possible?
your host must be able to run Python
https://github.com/google/zx can make it slightly easier, it's a wrapper around child_process
It won't be as simple as what you want but it's doable
@Eric Burel It won't be as simple as what you want but it's doable
In&OutOP
okay thanks mate
Australian Freshwater Crocodile
I think there are some new solutions like PyScript or something to run python on the client side. Never used it though
Idk if that's what you need
@Australian Freshwater Crocodile I think there are some new solutions like PyScript or something to run python on the client side. Never used it though
In&OutOP
yeah but i cant use it in nextjs can i?
@In&Out yeah but i cant use it in nextjs can i?
Australian Freshwater Crocodile
Why cant you use it in nextjs?
@Australian Freshwater Crocodile Why cant you use it in nextjs?
In&OutOP
i can, just figured it out, but its pretty messed up
i'll just make python backend
@In&Out i'll just make python backend
Dogo Argentino
Can you just make an api ?
I too have been building next js and fast api website
Let me know if need any help If i can contribute
I too have been building next js and fast api website
Let me know if need any help If i can contribute
Tho i have a question
so i have a file that will run every 15 seconds, it fetches a link then returns me a pandas dataframe, does the api live update?
or do i have to refresh api for it to update on frontend
@In&Out so i have a file that will run every 15 seconds, it fetches a link then returns me a pandas dataframe, does the api live update?
Dogo Argentino
file will run as in ...python script running every 15 to a link and fetch a Dataframe?
In&OutOP
yes
Dogo Argentino
will have to call api to refresh data
In&OutOP
hmm okay thanks
thats it
Dogo Argentino
api must be fetching the dataframe right
In&OutOP
yeah
need help
## page.tsx
"use client";
import { getInfo } from "@/components/fetch";
function Page() {
const { data, isLoading, isError } = getInfo();
console.log(data);
if (isLoading) {
return <p>Loading...</p>;
}
if (isError) {
return <p>Error: {isError.message}</p>;
}
if (!data) {
return <p>No data available</p>;
}
return <div>{data.map((bop:any)=>(
<div>{bop.id}</div>
))}</div>;
}
## fetch.tsx
import useSWR from "swr";
const fetcher = (url: any) => fetch(url).then((r) => r.json());
export function getInfo() {
const { data, error, isLoading } = useSWR(
"http://localhost:8000/api/users",
fetcher
);
return {
data,
isError: error,
isLoading,
};
}
Why when i fetch that path, i get undefined
even tho there is data
Dogo Argentino
cors error maybe ...
should allow req from your frontend local host
In&OutOP
but when i fetch in typicode it works
oh alr makes sense
Dogo Argentino
yeah the api will work can also check in postman ....but if you try calling it from other localhost port it wont
In&OutOP
yeah yeah forgot, will do now thanks
Dogo Argentino
need to allow req from whatever port frontend is hosted on
localhost/3000 default for next js
@Dogo Argentino localhost/3000 default for next js
In&OutOP
works thank you