Building a Vue.js SPA (Single Page Application) Frontend with FastAPI Backend for AI Integration
I wonder if I did not make an error of judgment…
As my objective remains to build a system likely to promote AI’s adoption, it pushed me to favor Streamlit as Frontend instead of building a real web application with a framework like Vue.js.
For this post also, you can find all files, mostly prompts, on my GitHub account. See https://github.com/bflaven/ia_usages/tree/main/ia_build_vue_js_on_fastapi
Unfortunately, the Streamlit web application is victim of its own success and stakeholders started to ask me for modifications on the frontend. As if it was much more a web application than an exploratory tool!
I am trapped, my main mission is not to flesh out a web application but more to implement IA features through an API. So, devoting time to the web application is to take the risk to get lost in the weeds!
Nevertheless, I did a quick assessment of how long it would take to replace Streamlit with Vue.js and connect it to the IA api built with Fastapi. This discovery is what this post is about.
First, here is the User story behind the scenes:
# prompt for this User Story
User Story: As a PO I want to build a web application propelled with Vue.js and not on Streamlit, aka the frontend to query an API built in Python with FastAPI aka the backend. The API delivered IA features.
This wish meets several objectives:
- Delegate the creation and maintenance of the web application to another team in order to relieve the AI team from frontend management. So, the AI’s team can focus on tasks with added value for AI e.g. fine-tuning, selecting models, etc.
- Pool knowledge internally so you are not isolating yourself in Python to make apps that you create and maintain elsewhere with other technologies.
- Re-enter into the digital device’s classic production cycle with all the stages that this implies: UX, Testing, CI…
For this post, I have defined a simple architecture for the application. The API instead of CRUD operations may handle IA functionalities but for educational purpose, I have simplified the objective, so the API consists of two main parts:
- Backend (FastAPI):
It provides RESTful APIs for CRUD operations and serves as the backend server for the application. - Frontend (Vue.js):
It is a single-page application (SPA) built using Vue.js to interact with the backend APIs and render the user interface.
The tech stack could be the following:
# Backend FastAPI SQLAlchemy & SQLite Pydantic JWT (JSON Web Tokens) # Frontend Vue.js Vuex Vuetify Axios
I made some prompts to help me out. See vite_vue_prompt_4.md, vite_vue_prompt_5.md
GitHub Projects as Exploration Milestones
As always, I searched the length and breadth of GitHub looking for projects containing a backend (FastAPI) and a frontend (Vue.js). Many projects are on Docker
- A very inspiring project where I have grabbed my User Story. https://github.com/krassykirov/FastAPI-Vue
- The most complete step-by-step walkthrough to achieve a SPA (Single Page App) with FastAPI and Vue.js. https://testdriven.io/blog/developing-a-single-page-app-with-fastapi-and-vuejs/
- A good knowledge refresher even if even the API is not made in Fastapi: “Fetching Data from a Third-party API with Vue.js and Axios.” https://www.sitepoint.com/fetching-data-third-party-api-vue-axios/
- A quick and dirty, very straightforward “Serve Vue from Fastapi in a breeze”. https://dimmaski.com/serve-vue-fastapi/
These two projects are much closer to my conditions of use. I have detailed extensively in previous posts, namely an AI device equipped with a backend (a FastAPI API which delivers AI functionalities) and a frontend in Vue.js and not in Streamilit.
- NHL Award Prediction Tracker Using Vue.js and FastAPI. https://github.com/jfbriggs/nhl-award-vue-fastapi/tree/master
- Simple image recognition using Vue 3 + Python fastAPI
Very instructive project even though the explanations are in Khmer! https://github.com/moking55/simple-vue-and-fastapi-face-recognizer
“Vite Vue”, some disparate commands as reminders to manipulate both backend and frontend
001_my_vue_project
# 001_my_vue_project - go to path cd /Users/brunoflaven/Documents/01_work/blog_articles/build_vue_js_on_fastapi/001_my_vue_project # Install the stuff npm install npm install --save-dev # launch the dev npm run dev # Check http://localhost:5173/ # launch the build npm run build npm run preview # Check http://localhost:4173/ # CAUTION: if you want to use the commnds vite npm install -g vite # For dev vite # For build vite build # For preview or serve vite preview
002_fastapi_vite
# 002_fastapi_vite cd /Users/brunoflaven/Documents/01_work/blog_articles/build_vue_js_on_fastapi/002_fastapi_vite # 1. BACKEND # start the backend (FastAPI) cd /Users/brunoflaven/Documents/01_work/blog_articles/build_vue_js_on_fastapi/002_fastapi_vite/api # launch the env source activate fmm_fastapi_poc # start the api uvicorn main:app --reload # Check http://127.0.0.1:8000/ # 2. FRONTEND # start the frontend (Vue.js) cd /Users/brunoflaven/Documents/01_work/blog_articles/build_vue_js_on_fastapi/002_fastapi_vite/ui # Install the stuff npm install npm install --save-dev # launch the dev npm run dev # Check http://localhost:5173/ # launch the build npm run build npm run preview # Check http://localhost:4173/ # CAUTION: if you want to use the commnds vite npm install -g vite # For dev vite # For build vite build
006_fastapi_database
Working backend with fastAPI
# 006_fastapi_database cd /Users/brunoflaven/Documents/01_work/blog_articles/build_vue_js_on_fastapi/006_my_vue_project/backend # launch the env source activate fmm_fastapi_poc # start the api uvicorn main:app --reload # Check http://127.0.0.1:8000/
012_fastapi_vue
# 012_fastapi_vue cd /Users/brunoflaven/Documents/01_work/blog_articles/build_vue_js_on_fastapi/012_fastapi_vue # 1. BACKEND # start the backend (FastAPI) cd /Users/brunoflaven/Documents/01_work/blog_articles/build_vue_js_on_fastapi/012_fastapi_vue/backend/src # launch the env source activate fmm_fastapi_poc # start the api uvicorn main:app --reload # Check http://127.0.0.1:8000/ # 2. FRONTEND # start the frontend (Vue.js) cd /Users/brunoflaven/Documents/01_work/blog_articles/build_vue_js_on_fastapi/012_fastapi_vue/frontend # Install the stuff npm install npm install --save-dev # launch the app, check package.json npm run serve npm run build npm run lint # Check http://localhost:4173/ # CAUTION: if you want to use the commnds vite npm install -g vite # For dev vite # For build vite build
the_vue_js_handbook
My concern was connecting the backend (API made with FastAPI) and the frontend (made with Vue.js), better know a little more how to handle a webapp in Vue.js. They are plenty of tutorials and ressources even well written books: Vue.js-3-By-Example has my preference: https://github.com/PacktPublishing/-Vue.js-3-By-Example
# path cd /Users/brunoflaven/Documents/01_work/blog_articles/build_vue_js_on_fastapi/the_vue_js_handbook/poc_vue_api # 1. CREATE A SERVER TO FAKE AN API # fake server npm install -g json-server npm install json-server --save-dev # path cd /Users/brunoflaven/Documents/01_work/blog_articles/build_vue_js_on_fastapi/the_vue_js_handbook/poc_vue_api/frontend_1/ # launch fake server json-server -p 5555 src/posts.json # Check http://localhost:5555/posts # 2. QUERY THIS FAKE API # install axios npm install axios --save-dev # launch the app npm run serve npm run build npm run lint # commands for Vues.js npm init vue@latest npm create vue@latest # command for Vite + Vue.js npm init vite@latest # path cd /Users/brunoflaven/Documents/01_work/blog_articles/build_vue_js_on_fastapi/ # install globally npm install -g @vue/cli # check vue vue -V # @vue/cli 5.0.8 # working with vue.js # path cd /Users/brunoflaven/Documents/01_work/blog_articles/build_vue_js_on_fastapi/ # create dir mkdir the_vue_js_handbook cd the_vue_js_handbook # create your first vue app in an html file touch your_first_vue_app.html # working_command_1 # vue createvue create frontend_2 # working_command_2 npm create vue@latest frontend_2 # other commands cd bf-fake-vue-app-1 npm install npm run format npm run dev
In package.json, change this to avoid common error for multi-word-component naming.
"rules": {"vue/multi-word-component-names": "off"}
A bit of Streamlit vs A bit of Vues.js
Being sensitive to the time-to-market, depriving yourself of Streamlit can be much more complex. I made three additional investigations with Streamlit and found easily solutions.In Vue.js, researching and implementing these solutions would undoubtedly have taken more time.
- streamlit_video_mp4 : For instance, I did quite a few transcription experiments using Whisper for both audio and video files. Naturally, retrieving transcription files with diarisation lead naturally to subtitles and using Streamlit to show a video with subtitles is extremely easy. Check “streamlit_video_mp4” in https://github.com/bflaven/ia_usages/tree/main/ia_build_vue_js_on_fastapi/streamlit_video_mp4
- streamlit_module_annotated_text: An attempt on “Annotated Text Component for Streamlit” by Thiago Teixeira, one of the founders of Streamlit. Check “streamlit_module_annotated_text” in https://github.com/bflaven/ia_usages/tree/main/ia_build_vue_js_on_fastapi/streamlit_module_annotated_text
- streamlit_no_reruns: Investigation on Streamlit question : “How to prevent the reloading of the whole page when I let the user perform an action ?”. Check “streamlit_no_reruns” in https://github.com/bflaven/ia_usages/tree/main/ia_build_vue_js_on_fastapi/streamlit_no_reruns
Here are some experiments made with Streamlit that prevents me from switching from Streamlit to Vue.js with no doubt.
Video #1 Scaffold Your First Vitejs App: Comprehensive Beginner’s Guide
Video #2 Master Vuejs: Build Your First App and Project with Ease
Video #3 Display API Data using Vuejs & Axios: Step-by-Step Tutorial
Video #4 Connect FastAPI Backend to Vue.js Frontend: Full Guide
Video #5 FastAPI Development: Boost Productivity with Database Boilerplates
Video #6 Streamlit Mastery: View Whisper Transcripts & Prevent Page Reloads
More infos
- Vue.js-3-By-Example
https://github.com/PacktPublishing/-Vue.js-3-By-Example - Vue 3 – CodeSandbox
https://codesandbox.io/s/vue - GitHub – bezkoder/vue-3-crud: Vue 3 example by a CRUD app with Axios, Vue Router, Bootstrap
https://github.com/bezkoder/vue-3-crud - The Vue Handbook: a thorough introduction to Vue.js
https://www.freecodecamp.org/news/the-vue-handbook-a-thorough-introduction-to-vue-js-1e86835d8446/ - How to Use Vue.js and Axios to Display Data from an API | DigitalOcean
https://www.digitalocean.com/community/tutorials/how-to-use-vue-js-and-axios-to-display-data-from-an-api - Fetching Data from a Third-party API with Vue.js and Axios â SitePoint
https://www.sitepoint.com/fetching-data-third-party-api-vue-axios/ - Rendering API Data with Vue.js and Axios – Techclaw
https://techclaw.org/rendering-api-data-with-vue-js-and-axios/ - How To Configure Vue.js REST API Consumption with Axios | DigitalOcean
https://www.digitalocean.com/community/tutorials/vuejs-rest-api-axios - Axios + Vue.js 3 + Pinia, a “comfy” configuration you can consider for an API REST – DEV Community
https://dev.to/bugintheconsole/axios-vuejs-3-pinia-a-comfy-configuration-you-can-consider-for-an-api-rest-1i5c - Rendering API Data with Vue.js and Axios | by TechClaw | Medium
https://medium.com/@techclaw/rendering-api-data-with-vue-js-and-axios-1e600c6f1031 - Build Vue.js, Vue Query, and Axios CRUD App with RESTful API 2024
https://codevoweb.com/vue-query-and-axios-crud-app/ - How to Create a web app using FastAPI, Vuejs and MongoDB for generating and showcasing images generated by Stability AI API Part 1 | by Sangeeth Joseph – The AI dev | Medium
https://medium.com/@sangeeth123sj/how-to-create-a-web-app-using-fastapi-vuejs-and-mongodb-for-generating-and-showcasing-images-193ccdb20091 - GitHub – pelocho/VueFastAPILearning
https://github.com/pelocho/VueFastAPILearning - GitHub – calvin-giese/VueFastAPIExample
https://github.com/calvin-giese/VueFastAPIExample - GitHub – pelocho/VueFastAPILearning
https://github.com/pelocho/VueFastAPILearning - GitHub – huyunlei/VueFast: Vue.js version 3 with FastAPI
https://github.com/huyunlei/VueFast - GitHub – jitsejan/fastapi-sqlalchemy-tutorial: Simple tutorial setting up FastAPI with SQLAlchemy with a VueJS frontend.
https://github.com/jitsejan/fastapi-sqlalchemy-tutorial - GitHub – dwisulfahnur/blog-fastapi-vuejs: Simple project that I write using fastapi, motor, and umongo for the backend. VueJS on the frontend
https://github.com/dwisulfahnur/blog-fastapi-vuejs - GitHub – frodrig3ND/DailyDashV2
https://github.com/frodrig3ND/DailyDashV2 - How To Configure Vue.js REST API Consumption with Axios | DigitalOcean
https://www.digitalocean.com/community/tutorials/vuejs-rest-api-axios - Axios + Vue.js 3 + Pinia, a “comfy” configuration you can consider for an API REST – DEV Community
https://dev.to/bugintheconsole/axios-vuejs-3-pinia-a-comfy-configuration-you-can-consider-for-an-api-rest-1i5c - Connecter un frontend SPA Vue.js à une API backend – Julien Salinas
https://juliensalinas.com/fr/connecter-SPA-frontend-vuejs-axios-API-backend/ - Rendering API Data with Vue.js and Axios | by TechClaw | Medium
https://medium.com/@techclaw/rendering-api-data-with-vue-js-and-axios-1e600c6f1031 - Building an E-Commerce app with Vue.js, Vuex & Axios
https://codesource.io/blog/building-an-e-commerce-app-with-vue-js-vuex-axios/ - GitHub – moking55/simple-vue-and-fastapi-face-recognizer
https://github.com/moking55/simple-vue-and-fastapi-face-recognizer - GitHub – kotaaaa/employees-app: Simple CRUD Application with FastAPI(Python), Vue.js, MySQL, docker-compose
https://github.com/kotaaaa/employees-app - Developing a Single Page App with FastAPI and Vue.js | TestDriven.io
https://testdriven.io/blog/developing-a-single-page-app-with-fastapi-and-vuejs/ - Integrating FastAPI and React: Building a Full-Stack Application – Spencer Sharp
https://www.haydenbarnett.dev/articles/integrating-fastapi-and-react - GitHub – krassykirov/FastAPI-Vue: This project is a test of developing backend and frontend web app with FastAPI and Vue.js
https://github.com/krassykirov/FastAPI-Vue - How To Build and Deploy a Machine Learning Model with FastAPI | Better Data Science
https://betterdatascience.com/deploy-a-machine-learning-model-with-fastapi/ - How To Build and Deploy a Machine Learning Model with FastAPI | Better Data Science
https://betterdatascience.com/deploy-a-machine-learning-model-with-fastapi/ - GitHub – jfbriggs/nhl-award-vue-fastapi: Full-stack application (Vue.js + FastAPI) implementing machine learning to predict NHL regular-season award outcomes.
https://github.com/jfbriggs/nhl-award-vue-fastapi/tree/master - GitHub – krassykirov/FastAPI-Vue: This project is a test of developing backend and frontend web app with FastAPI and Vue.js
https://github.com/krassykirov/FastAPI-Vue - GitHub – dwisulfahnur/blog-fastapi-vuejs: Simple project that I write using fastapi, motor, and umongo for the backend. VueJS on the frontend
https://github.com/dwisulfahnur/blog-fastapi-vuejs - GitHub – serj2626/fapi-vue-interviews: Project “List of Interviews” on FastAPI and Vue
https://github.com/serj2626/fapi-vue-interviews - GitHub – najeeb67/Hospital-Project-in-Python-and-Js: Its Project About Hospital in FastApi And Vue.js
https://github.com/najeeb67/Hospital-Project-in-Python-and-Js - GitHub – joaovitoriasilva/endurain: My DIY fitness tracker journey. Frontend is built using Vue.js and Bootstrap CSS; Python FastAPI, Alembic, SQLAlchemy, stravalib, gpxpy, MariaDB behind the scenes. Suggestions welcome!
https://github.com/joaovitoriasilva/endurain - GitHub – tiangolo/full-stack-fastapi-template: Full stack, modern web application template. Using FastAPI, React, SQLModel, PostgreSQL, Docker, GitHub Actions, automatic HTTPS and more.
https://github.com/tiangolo/full-stack-fastapi-template - GitHub – wpcodevo/fastapi_sqlalchemy: In this article, I’ll provide you with a simple and straightforward guide on how you can build a CRUD app with FastAPI and SQLAlchemy. The FastAPI app will run on a Starlette web server, use Pydantic for data validation, and store data in an SQLite database.
https://github.com/wpcodevo/fastapi_sqlalchemy - GitHub – KenMwaura1/Fast-Api-Vue: Simple asynchronous API implemented with Fast-Api framework utilizing Postgres as a Database and SqlAlchemy as ORM . GiHub Actions as CI/CD Pipeline. Vue + Daisy UI for the frontend
https://github.com/KenMwaura1/Fast-Api-Vue/tree/main - GitHub – martinrenner/RENBoard: FastAPI + React(Vite) + PostgresSQL + PgAdmin
https://github.com/martinrenner/RENBoard - GitHub – cofin/fastapi-vite: Template helpers for including assets generated by Vite JS. Includes basic support FastAPI / Starlette templates.
https://github.com/cofin/fastapi-vite - GitHub – SamuelEarl/svelte-fastapi-demo-app: Demo App Showing How To Configure Svelte (using Vite) with FastAPI
https://github.com/SamuelEarl/svelte-fastapi-demo-app - GitHub – venkyPy-2019/FastAPI: FastApi, python and react (VITE)
https://github.com/venkyPy-2019/FastAPI - GitHub – eno-conan/vite-fastapi_sqlalchemy: vite-fastapi_sqlalchemy
https://github.com/eno-conan/vite-fastapi_sqlalchemy/tree/main - The Ultimate FastAPI Tutorial Part 12 – Setting Up a React Frontend
https://christophergs.com/tutorials/ultimate-fastapi-tutorial-pt-12-react-js-frontend/ - Developing a Single Page App with FastAPI and React | TestDriven.io
https://testdriven.io/blog/fastapi-react/ - GitHub – MehdiRtal/nuxt-fastapi-template: Full-Stack modern web application template. WIP
https://github.com/MehdiRtal/nuxt-fastapi-template/tree/main - GitHub – junkei-okinawa/hasura-postgres-fastapi-nuxtjs-vuetify: Template docker-compose.
https://github.com/junkei-okinawa/hasura-postgres-fastapi-nuxtjs-vuetify - GitHub – Sakops/Nuxtjs-with-FastAPI: implementing nuxtjs with FastAPI
https://github.com/Sakops/Nuxtjs-with-FastAPI - GitHub – rasheedjdh/vuejs-nuxtjs-fastapi
https://github.com/rasheedjdh/vuejs-nuxtjs-fastapi - GitHub – it21024818/CafeGreenRestuarentManagementSystem: Cafeteria Management System
https://github.com/it21024818/CafeGreenRestuarentManagementSystem - GitHub – TutorFx/nuxtjs-fastapi: FastAPI & Nuxt.js Boilerplate
https://github.com/TutorFx/nuxtjs-fastapi - Three simple ways to run Vue.js along side FastAPI | by Jordan P. Raychev | CodeX | Medium
https://medium.com/codex/how-to-integrate-fastapi-with-vuejs-ef6734b308fd - fastapi-vue/services at main · testdrivenio/fastapi-vue · GitHub
https://github.com/testdrivenio/fastapi-vue/tree/main/services - Serve Vue from Fastapi in a breeze – dimmaski
https://dimmaski.com/serve-vue-fastapi/ - Build an API App backed by FastAPI and Vue.js – Chaoran’s Data Story
https://www.ichaoran.com/data-story/visualization/data-engineering/fastapi-vue-app/ - Build an AI-based Autocomplete in the Browser using Vue.js, FastAPI, and WebSockets | by Neeraj Krishna | Towards Data Science
https://towardsdatascience.com/build-an-ai-based-autocomplete-in-the-browser-using-vue-js-fastapi-and-websockets-1eb7ae19bfd8 - How To Deploy Machine Learning Models Using Fast API | The Analytics Club
https://www.the-analytics.club/deploy-machine-learning-models-using-fast-api/