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,
cd
into your project folder. - In your terminal, type
npm init @vitejs/app
. Give your Vite project the nameclient
then 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:
cd
into theserver
directory 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,
cd
into theserver
directory and enterdeactivate
.
- Create a
main.py
file inside yourserver
directory and copy the code from themain.py
file 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:
cd
into theclient
directory and runnpm install
. - You should install Python dependencies inside your virtualenv. You can create a
requirements.txt
file inside theserver
directory that contains arequirements.txt
file. If you use arequirements.txt
file for dependencies, thencd
into yourserver
directory and usepip3
to 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
server
directory and installing dependencies for FastAPI. - Run Uvicorn server for FastAPI with hotreloading:
cd
into theserver
directory and runuvicorn main:app --reload
- Run the Svelte app in development mode and watch for changes: Open a terminal window and
cd
into theclient
directory and runnpm run dev
- Build the client-side code for production:
cd
into theclient
directory and runnpm run build
. That will create a newclient/dist
directory that contains bundled and optimized JavaScript, CSS, and image files. - Run the Uvicorn server:
cd
into theserver
directory and runuvicorn main:app
. The server should now be serving up the client side code for theclient/dist
directory. 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.