Next.js Discord

Discord Forum

Nextjs and python

Answered
In&Out posted this in #help-forum
Open in Discord
Avatar
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

Avatar
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
Avatar
@Eric Burel I've tried something like that https://github.com/lbke/snext
Avatar
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?
Answer
Avatar
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
Avatar
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
Avatar
@In&Out yeah but i cant use it in nextjs can i?
Avatar
Australian Freshwater Crocodile
Why cant you use it in nextjs?
Avatar
@Australian Freshwater Crocodile Why cant you use it in nextjs?
Avatar
i can, just figured it out, but its pretty messed up
i'll just make python backend
Avatar
@In&Out i'll just make python backend
Avatar
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
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
Avatar
@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?
Avatar
Dogo Argentino
file will run as in ...python script running every 15 to a link and fetch a Dataframe?
Avatar
yes
Avatar
Dogo Argentino
will have to call api to refresh data
Avatar
hmm okay thanks
thats it
Avatar
Dogo Argentino
api must be fetching the dataframe right
Avatar
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
Image
even tho there is data
Avatar
Dogo Argentino
cors error maybe ...
should allow req from your frontend local host
Avatar
but when i fetch in typicode it works
oh alr makes sense
Avatar
Dogo Argentino
yeah the api will work can also check in postman ....but if you try calling it from other localhost port it wont
Avatar
yeah yeah forgot, will do now thanks
Avatar
Dogo Argentino
need to allow req from whatever port frontend is hosted on
localhost/3000 default for next js
Avatar
@Dogo Argentino localhost/3000 default for next js
Avatar
works thank you