This app shows how to configure a Svelte SPA app. This configuration would come into play, for example, in scenarios where you have a backend that is not written in Node.js and/or you don't need SSR.
cd into your project folder.npm init @vitejs/app. Give your Vite project the name client then select the Svelte template. This will create your Vite/Svelte project inside a directory named client. server.cd into the server directory then run source demo-app-venv/bin/activate. cd into the server directory and enter deactivate.main.py file inside your server directory and copy the code from the main.py file in this repository into your file.If you clone this repo, then you will have to install the dependencies for the client and the server.
cd into the client directory and run npm install.requirements.txt file inside the server directory that contains a requirements.txt file. If you use a requirements.txt file for dependencies, then cd into your server directory and use pip3 to install the dependencies using Python3: pip3 install -r requirements.txtMake sure that you create a virtual environment for your Python code (everything inside of the server directory) and have it running while you are developing your app. See this tutorial: Installing and using virtualenv with Python 3
The name of this project is demo-app so I named the virtual environment demo-app-venv. In order to run the virtual environment you will run this command from the server directory:
source demo-app-venv/bin/activate
It is important that you run the following commands in this order otherwise the Vite dev server (which is used in the frontend code) will error out if there is no Uvicorn server running on port 8000.
server directory and installing dependencies for FastAPI.cd into the server directory and run uvicorn main:app --reloadcd into the client directory and run npm run devcd into the client directory and run npm run build. That will create a new client/dist directory that contains bundled and optimized JavaScript, CSS, and image files.cd into the server directory and run uvicorn main:app. The server should now be serving up the client side code for the client/dist directory. Open a browser and navigate to localhost:8000. You should see the app in the browser.The vite.config.js file is where the configurations are located for proxying frontend requests to the backend server. The main.py file also includes CORS configurations to allow requests from the frontend during development because the frontend code will run on a different port during development.