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.
- Create a folder on your computer where you want to store your project code.
- Client-side Setup:
- From a terminal window,
cdinto your project folder. - In your terminal, type
npm init @vitejs/app. Give your Vite project the nameclientthen select the Svelte template. This will create your Vite/Svelte project inside a directory namedclient.- NOTE: The above instructions are located at vitejs.dev. Click "Get Started" then follow the instructions under "Scaffolding Your First Vite Project".
- From a terminal window,
- Server-side Setup:
- Create another folder inside your project folder called
server. - Follow the instructions below to create a virtual environment.
- Make sure your virtual environment is running:
cdinto theserverdirectory then runsource demo-app-venv/bin/activate.- Make sure to install all Python packages for this project while the virtual environment is running.
- To deactivate the Python virtual environment,
cdinto theserverdirectory and enterdeactivate.
- Create a
main.pyfile inside yourserverdirectory and copy the code from themain.pyfile in this repository into your file.
- Create another folder inside your project folder called
If you clone this repo, then you will have to install the dependencies for the client and the server.
- Install Node dependencies for the client:
cdinto theclientdirectory and runnpm install. - You should install Python dependencies inside your virtualenv. You can create a
requirements.txtfile inside theserverdirectory that contains arequirements.txtfile. If you use arequirements.txtfile for dependencies, thencdinto yourserverdirectory and usepip3to install the dependencies using Python3:pip3 install -r requirements.txt
Make 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.
- Follow the instructions above for running your virtual environment from the
serverdirectory and installing dependencies for FastAPI. - Run Uvicorn server for FastAPI with hotreloading:
cdinto theserverdirectory and runuvicorn main:app --reload - Run the Svelte app in development mode and watch for changes: Open a terminal window and
cdinto theclientdirectory and runnpm run dev
- Build the client-side code for production:
cdinto theclientdirectory and runnpm run build. That will create a newclient/distdirectory that contains bundled and optimized JavaScript, CSS, and image files. - Run the Uvicorn server:
cdinto theserverdirectory and runuvicorn main:app. The server should now be serving up the client side code for theclient/distdirectory. Open a browser and navigate tolocalhost: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.