Next.js Discord

Discord Forum

Pyscript with nextjs

Answered
In&Out posted this in #help-forum
Open in Discord
Avatar
Hey, is it possible to use pyscript with nextjs?
Answered by In&Out
import { useEffect, useState } from "react";

export default function Home() {
  const [hydrated, setHydrated] = useState(false);
  useEffect(() => {
    setHydrated(true);
  }, []);
  if (!hydrated) {
    // Returns null on first render, so the client and server match
    return null;
  }
  return (
    <div>
      test
      <py-script>
        for i in range(9):{"\n"} print(i){"\n"}
        {"\n"}
        def func():{"\n"} print('function works'){"\n"}
      </py-script>
    </div>
  );
}
View full answer

38 Replies

Avatar
What did you try, what did work/didn't work
it's supposed to work in the broswser, so it probably works, but you'll have to explore how
Avatar
import type { Metadata } from "next";
import { Inter } from "next/font/google";
import "./globals.css";
import Script from "next/script";
import { NextScript } from "next/document";

const inter = Inter({ subsets: ["latin"] });

export const metadata: Metadata = {
  title: "Create Next App",
  description: "Generated by create next app",
};

export default function RootLayout({
  children,
}: Readonly<{
  children: React.ReactNode;
}>) {
  return (
    <html lang="en">
      <link rel="stylesheet" href="https://pyscript.net/latest/pyscript.css" />
      <Script
        defer
        src="https://pyscript.net/latest/pyscript.js"
        strategy="beforeInteractive"
      />
      <body className={inter.className}>
        {children}
        <NextScript />
      </body>
    </html>
  );
}
a dumb try ngl lol
Avatar
cool, did you try to run some Python with that ?
Avatar
Image
Avatar
semmes to be blending Pages and App Router, can you try on a freshly created Next.js app maybe ?
Avatar
i did
Avatar
are you sure the issue is related with PyScript?
Avatar
let me remove it
yup, it is
Avatar
what's NextScript from "next/document" ?
Avatar
works without it
Avatar
this tutorial is for the pages router you should try with the pages router rather than the app router then
Avatar
ah alr, how to get pages folder in next 14?
Avatar
mkdir pages
rm app
it its retrocompatible
Avatar
oh just that?
alr will do
Avatar
yep pages router is still working fine and will for a long while
Avatar
Image
like this?
will just reinstall
Avatar
did it, tho it doesnt work
export default function Home() {
  return (
    <div>
      test
      <py-script>
        for i in range(9): print(i) def func(): print('function works')
      </py-script>
    </div>
  );
}
Image
Property 'py-script' does not exist on type 'JSX.IntrinsicElements'.
do i need to downgrade nextjs or somethig
Avatar
oh crap, it works
Avatar
import { useEffect, useState } from "react";

export default function Home() {
  const [hydrated, setHydrated] = useState(false);
  useEffect(() => {
    setHydrated(true);
  }, []);
  if (!hydrated) {
    // Returns null on first render, so the client and server match
    return null;
  }
  return (
    <div>
      test
      <py-script>
        for i in range(9):{"\n"} print(i){"\n"}
        {"\n"}
        def func():{"\n"} print('function works'){"\n"}
      </py-script>
    </div>
  );
}
Answer
Avatar
This works
Image
somehow, lol but it works
Avatar
yeah I was expecting something like that you need client-only components
Avatar
okay thanks