When I serve the code locally using Vite and run the example.py file, it works as expected. In other words, when I have _RELEASE = False in __init__.py and run npm run dev and streamlit run example.py I get the following output:
However, when I compile the files with npm run build, switch to _RELEASE = True and run the example file again, the component itself doesn’t render and after a couple os seconds an error appears:
Inspecting the browser, it shows that it failed to load both the javascript and the css compiled files. Somehow it cannot find them. But I checked their paths and they seem to be ok.
Go to the repository root directory, create a new Python virtual environment, activate it and install Streamlit and the template as an editable package:
I also checked if the compiled files are working. According to Vite’s documentation:
Once you’ve built the app, you may test it locally by running npm run preview command.
npm run build
npm run preview
The vite preview command will boot up a local static web server that serves the files from dist at http://localhost:4173 . It’s an easy way to check if the production build looks OK in your local environment.
So, with the release flag still set to True, I changed __init__.py line 41 from:
path=build_dir
To:
url="http://localhost:4173"
And the component worked perfectly again. It’s important to note that this method is not meant as a production server. Thus, this is not a solution to the problem, I still need to know how to serve properly from path=build_dir.
I had a very similar problem recently, so not sure if this thing will work for you or not but I did this for rendering react with vite inside streamlit.
add a console.log(window.location.href) inside your app.tsx file, this will print something a url: http://localhost:8501/component/.... now go to your vite.config.ts and inside that add this /component/... url in the public path, and this will work:
this above explanation is for the reactjs, but you can modify it for vue.js.
one problem you might face is the initial rendering of the react goes to 404 page, like not the base url so I am still figuring out the solution for this.