March 18th, 2025

Build a Streamlit application with Fabric SQL Database

Sunitha Muthukrishna
Principal Product Manager

Building an application allows you to operationalize the data you have in Microsoft Fabric, such that it can be consumed by stakeholders in your organization to generate business insights.  Streamlit is easy to use to build simple applications without needing to know front-end application frameworks like Flask, Django etc. It makes it easier to prototype and visualize your data in Microsoft Fabric with Streamlit framework.

In this blog post we will show how to develop an app using Streamlit and Fabric SQL database. Consider a scenario that you are a data engineer in a retail company and want to keep track of your product inventory. With a Streamlit app, you can connect to your Fabric SQL database where all the inventory data is stored and then create a really cool app that lets you see which products are running low, which ones are selling fast, and even predict future inventory needs.

Prerequisites

  1. You need an existing Fabric capacity. If you don’t have one, start a Fabric trial.
  2. Make sure that you Enable SQL database in Fabric using Admin Portal tenant settings.
  3. Install Visual Studio code, Python runtime and Python extension for VS Code on our local machine.

Create a workspace

  1. Sign in to Microsoft Fabric .
  2. Select Workspaces > New workspace.
  3. Fill out the Create a workspace. 
  4. In the Advanced section, choose Fabric capacity or Trial in the License mode section, or choose a premium capacity you have access to.
  5. Select Apply to complete creation of the workspace.

Create your first SQL database

  1. Ensure you are in the Workspace you created earlier by selecting the Workspaces icon in the navigation bar, and then selecting the Workspace you created in the last step.
  2. Create a Fabric SQL database by selecting the + New item.
  3. In the New Item | All Items panel, scroll to the Store Data area and select SQL database (preview).
  4. Open the SQL database in Fabric database you created in the last tutorial step. You can find it in the navigation bar of the Fabric portal, or by finding it in your Workspace for this tutorial.
  5. Select the Sample data button. This takes a few moments to populate your tutorial database with the SalesLT sample data.
  6. Once the sample data is imported, SalesLT schema and associated tables are added to the object explorer.
  7. Select the settings->Connection string to get the ODBC connection string for the SQL Database.

Develop Streamlit app

Use Visual Studio code for developing the application.

  1. Create a folder to work on this project. Open it in VS Code.
  2. Create Python virtual environment in your working directory.
  3. Create requirements.txt file. Add the following libraries
streamlit
pandas
pyodbc
  1. Run the following command in VS Code terminal to setup all the requirements.,
pip install -r requirements.txt
  1. Create app.py file and copy this code into the file.
import streamlit as st
import pyodbc
import pandas as pd
import os
import struct

# Database Connection Settings
# ODBC Connection String
CONN_STR = (
    'REPLACE_WITH_ODBC_CONNECTION_STRING'
)

# Function to connect to Azure SQL
def connect_to_db():
    try:
        conn = pyodbc.connect(CONN_STR)
        return conn
    except Exception as e:
        st.error(f"Database connection failed: {str(e)}")
        return None
# Streamlit UI

st.title("Fabric SQL Streamlit App")
if st.button("Fetch Data"):
    conn = connect_to_db()
    st.header("Show all products and description", divider="gray")
if conn:
        query = "SELECT TOP (100) * FROM [SalesLT]. [vProductAndDescription]"
      df = pd.read_sql(query, conn)
      st.write(df)
      st.title("📊 Simple Bar Chart Example")
      getproductsbymodel= "SELECT top 5 ProductModel, ProductCount=COUNT(*)  FROM [SalesLT].[vProductAndDescription] GROUP by ProductModel order by ProductCount DESC"
      chartdf = pd.read_sql(getproductsbymodel, conn)

      # Bar Chart
      st.subheader("Get Products inventory by Model:")
      st.bar_chart(chartdf.set_index('ProductModel'))
      # Display Data
      st.subheader("Data:")
      st.dataframe(chartdf)
      # Close Connection
      conn.close()
    else:
      st.error("Database connection failed.")

Test locally

Open a terminal in VS code and run python -m streamlit run app.py  . You will the similar output in the terminal window.

(.venv) C:\streamlitappdemo>python -m streamlit run app.py

  You can now view your Streamlit app in your browser.

  Local URL: http://localhost:8501

  Network URL: http://10.12.110.104:8501

 

Select the local URL and run it in your browser. Select Fetch data and this will ask you to authenticate to valid your permissions to the database before fetching the results. Your app will show the data return

  • Table with all the products
  • Bar chart showing count of products by Model type

Image Streamlit app with Fabric SQL DB
Streamlit app with Fabric SQL DB

 

Conclusion

These visualizations offer a clear and concise view of your data, making it easier to derive meaningful insights. By leveraging Streamlit and Fabric SQL database, you can create powerful data-driven applications quickly and efficiently.  To make this production ready and deploy to Azure web app , see how to deploy Streamlit app to Azure web apps.

 

 

Author

Sunitha Muthukrishna
Principal Product Manager

With over 12 years of experience in product management, I currently focus on developer experiences in Microsoft Fabric. I specialized in driving key business metrics such as growth, retention, and acquisition. I spearheaded initiatives that enhance customer experiences for Azure Databases.

0 comments