This guide will walk you through the process running a Python application using FastAPI, which interacts with a Microsoft SQL Server database using PyODBC.
Before getting started, make sure you have the following prerequisites installed on your system:
- source_image_reference = { publisher = "Canonical" offer = "0001-com-ubuntu-server-focal" sku = "20_04-lts" version = "latest" }
- Python
- pip
Clone the application's source code from your version control system or download it as a zip archive and extract it to your local machine.
git clone <repository_url>
cd <repository_directory>
Edit the app.py
file to update the connection_string
variable with the appropriate connection details for your SQL Server database. Update ODBC Version to 17
To Run the Application, open a terminal, navigate to the project directory, and run the following command:
pip install -r requirements.txt
sudo su
apt-get update && apt-get install -y unixodbc unixodbc-dev
curl https://packages.microsoft.com/keys/microsoft.asc | apt-key add -
curl https://packages.microsoft.com/config/debian/10/prod.list > /etc/apt/sources.list.d/mssql-release.list
apt-get update
ACCEPT_EULA=Y apt-get install -y msodbcsql17
uvicorn app:app --host 0.0.0.0 --port 8000
Your Python application is now running. You can access it by opening a web browser or sending HTTP requests to http://localhost:8000
or by using VM's Public IP.
/tasks
: List all tasks (GET)/tasks/{task_id}
: Retrieve a single task by ID (GET)/tasks
: Create a new task (POST)/tasks/{task_id}
: Update an existing task by ID (PUT)/tasks/{task_id}
: Delete a task by ID (DELETE)
You've successfully run a Python application. Feel free to make changes to the application and deploy it to your preferred environment.