{"id":4361,"date":"2025-03-18T12:19:41","date_gmt":"2025-03-18T19:19:41","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/azure-sql\/?p=4361"},"modified":"2025-03-18T12:19:41","modified_gmt":"2025-03-18T19:19:41","slug":"build-a-streamlit-application-with-fabric-sql-database","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/azure-sql\/build-a-streamlit-application-with-fabric-sql-database\/","title":{"rendered":"Build a Streamlit application with Fabric SQL Database"},"content":{"rendered":"<p>Building an application allows you to operationalize the data you have in <a href=\"https:\/\/learn.microsoft.com\/en-us\/fabric\/\">Microsoft Fabric<\/a>, such that it can be consumed by stakeholders in your organization to generate business insights. \u00a0<a href=\"https:\/\/streamlit.io\/\">Streamlit<\/a> 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.<\/p>\n<p>In this blog post we will show how to develop an app using <a href=\"https:\/\/streamlit.io\/\">Streamlit<\/a> and <a href=\"https:\/\/learn.microsoft.com\/en-us\/fabric\/database\/sql\/overview\">Fabric SQL database<\/a>. 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 <a href=\"https:\/\/learn.microsoft.com\/en-us\/fabric\/database\/sql\/overview\">Fabric SQL database<\/a> 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.<\/p>\n<h2><strong>Prerequisites<\/strong><\/h2>\n<ol>\n<li>You need an existing Fabric capacity. If you don&#8217;t have one,\u00a0<a href=\"https:\/\/learn.microsoft.com\/en-us\/fabric\/fundamentals\/fabric-trial\">start a Fabric trial<\/a>.<\/li>\n<li>Make sure that you\u00a0<a href=\"https:\/\/learn.microsoft.com\/en-us\/fabric\/database\/sql\/enable\">Enable SQL database in Fabric using Admin Portal tenant settings<\/a>.<\/li>\n<li>Install <a href=\"https:\/\/code.visualstudio.com\/\">Visual Studio code<\/a>, <a href=\"https:\/\/www.python.org\/downloads\/\">Python runtime<\/a> and <a href=\"https:\/\/code.visualstudio.com\/docs\/languages\/python\">Python extension for VS Code<\/a> on our local machine.<\/li>\n<\/ol>\n<h2><strong>Create a workspace<\/strong><\/h2>\n<ol>\n<li>Sign in to\u00a0<a href=\"https:\/\/app.fabric.microsoft.com\/\">Microsoft Fabric <\/a>.<\/li>\n<li>Select\u00a0<strong>Workspaces<\/strong>\u00a0&gt;\u00a0<strong>New workspace<\/strong>.<\/li>\n<li>Fill out the\u00a0<strong>Create a workspace.\u00a0<\/strong><\/li>\n<li>In the <strong>Advanced<\/strong> section, choose\u00a0<strong>Fabric capacity<\/strong>\u00a0or\u00a0<strong>Trial<\/strong>\u00a0in the\u00a0<strong>License mode<\/strong>\u00a0section, or choose a premium capacity you have access to.<\/li>\n<li>Select\u00a0<strong>Apply <\/strong>to complete creation of the workspace.<\/li>\n<\/ol>\n<h2><strong>Create your first SQL database<\/strong><\/h2>\n<ol>\n<li>Ensure you are in the Workspace you created earlier by selecting the\u00a0<strong>Workspaces<\/strong>\u00a0icon in the navigation bar, and then selecting the Workspace you created in the last step.<\/li>\n<li>Create a Fabric SQL database by selecting the\u00a0<strong>+ New item<\/strong>.<\/li>\n<li>In the\u00a0<strong>New Item | All Items<\/strong>\u00a0panel, scroll to the\u00a0<strong>Store Data<\/strong>\u00a0area and select\u00a0<strong>SQL database (preview)<\/strong>.<\/li>\n<li>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.<\/li>\n<li>Select the\u00a0<strong>Sample data<\/strong>\u00a0button. This takes a few moments to populate your tutorial database with the\u00a0<em>SalesLT<\/em>\u00a0sample data.<\/li>\n<li>Once the sample data is imported, SalesLT\u00a0schema and associated tables are added to the object explorer.<\/li>\n<li>Select the <strong>settings-&gt;Connection string <\/strong>to get the <strong>ODBC connection string<\/strong> for the SQL Database.<\/li>\n<\/ol>\n<h2>Develop Streamlit app<\/h2>\n<p>Use <a href=\"https:\/\/code.visualstudio.com\/\">Visual Studio code<\/a> for developing the application.<\/p>\n<ol>\n<li>Create a folder to work on this project. Open it in VS Code.<\/li>\n<li>Create <a href=\"https:\/\/code.visualstudio.com\/docs\/python\/environments\">Python virtual environment<\/a> in your working directory.<\/li>\n<li>Create <strong>requirements.txt<\/strong> file. Add the following libraries<\/li>\n<\/ol>\n<pre style=\"padding-left: 80px\">streamlit\r\npandas\r\npyodbc<\/pre>\n<ol start=\"2\">\n<li>Run the following command in VS Code terminal to setup all the requirements.,<\/li>\n<\/ol>\n<pre style=\"padding-left: 40px\">pip install -r requirements.txt<\/pre>\n<ol start=\"3\">\n<li>Create <strong>app.py<\/strong> file and copy this code into the file.<\/li>\n<\/ol>\n<pre style=\"padding-left: 40px\">import streamlit as st\r\nimport pyodbc\r\nimport pandas as pd\r\nimport os\r\nimport struct\r\n\r\n# Database Connection Settings\r\n# ODBC Connection String\r\nCONN_STR = (\r\n  \u00a0 'REPLACE_WITH_ODBC_CONNECTION_STRING'\r\n)\r\n\r\n# Function to connect to Azure SQL\r\ndef connect_to_db():\r\n  \u00a0 try:\r\n  \u00a0 \u00a0 \u00a0 conn = pyodbc.connect(CONN_STR)\r\n\u00a0 \u00a0 \u00a0 \u00a0 return conn\r\n  \u00a0 except Exception as e:\r\n  \u00a0 \u00a0 \u00a0 st.error(f\"Database connection failed: {str(e)}\")\r\n  \u00a0 \u00a0 \u00a0 return None\r\n# Streamlit UI\r\n\r\nst.title(\"Fabric SQL Streamlit App\")\r\nif st.button(\"Fetch Data\"):\r\n  \u00a0 conn = connect_to_db()\r\n    st.header(\"Show all products and description\", divider=\"gray\")\r\nif conn:\r\n\u00a0 \u00a0 \u00a0 \u00a0 query = \"SELECT TOP (100) * FROM [SalesLT]. [vProductAndDescription]\"\r\n\u00a0 \u00a0 \u00a0 df = pd.read_sql(query, conn)\r\n\u00a0 \u00a0 \u00a0 st.write(df)\r\n  \u00a0 \u00a0 st.title(\"\ud83d\udcca Simple Bar Chart Example\")\r\n  \u00a0 \u00a0 getproductsbymodel= \"SELECT top 5 ProductModel, ProductCount=COUNT(*) \u00a0FROM [SalesLT].[vProductAndDescription] GROUP by ProductModel order by ProductCount DESC\"\r\n  \u00a0 \u00a0 chartdf = pd.read_sql(getproductsbymodel, conn)\r\n\r\n  \u00a0 \u00a0 # Bar Chart\r\n  \u00a0 \u00a0 st.subheader(\"Get Products inventory by Model:\")\r\n  \u00a0 \u00a0 st.bar_chart(chartdf.set_index('ProductModel'))\r\n  \u00a0 \u00a0 # Display Data\r\n  \u00a0 \u00a0 st.subheader(\"Data:\")\r\n  \u00a0 \u00a0 st.dataframe(chartdf)\r\n  \u00a0 \u00a0 # Close Connection\r\n  \u00a0 \u00a0 conn.close()\r\n  \u00a0 else:\r\n  \u00a0 \u00a0 st.error(\"Database connection failed.\")\r\n\r\n<\/pre>\n<h2>Test locally<\/h2>\n<p>Open a terminal in VS code and run <em>python -m streamlit run app.py<\/em> \u00a0. You will the similar output in the terminal window.<\/p>\n<pre>(.venv) C:\\streamlitappdemo&gt;python -m streamlit run app.py\r\n\r\n\u00a0 You can now view your Streamlit app in your browser.\r\n\r\n\u00a0\u00a0Local URL: http:\/\/localhost:8501\r\n\r\n\u00a0 Network URL: http:\/\/10.12.110.104:8501<\/pre>\n<p>&nbsp;<\/p>\n<p>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<\/p>\n<ul>\n<li>Table with all the products<\/li>\n<li>Bar chart showing count of products by Model type<\/li>\n<\/ul>\n<p><figure id=\"attachment_4397\" aria-labelledby=\"figcaption_attachment_4397\" class=\"wp-caption aligncenter\" ><a href=\"https:\/\/devblogs.microsoft.com\/azure-sql\/wp-content\/uploads\/sites\/56\/2025\/03\/Streamlit-app-with-Fabric-SQL-DB.png\"><img decoding=\"async\" class=\"wp-image-4397 size-large\" src=\"https:\/\/devblogs.microsoft.com\/azure-sql\/wp-content\/uploads\/sites\/56\/2025\/03\/Streamlit-app-with-Fabric-SQL-DB-1024x576.png\" alt=\"Image Streamlit app with Fabric SQL DB\" width=\"1024\" height=\"576\" srcset=\"https:\/\/devblogs.microsoft.com\/azure-sql\/wp-content\/uploads\/sites\/56\/2025\/03\/Streamlit-app-with-Fabric-SQL-DB-1024x576.png 1024w, https:\/\/devblogs.microsoft.com\/azure-sql\/wp-content\/uploads\/sites\/56\/2025\/03\/Streamlit-app-with-Fabric-SQL-DB-300x169.png 300w, https:\/\/devblogs.microsoft.com\/azure-sql\/wp-content\/uploads\/sites\/56\/2025\/03\/Streamlit-app-with-Fabric-SQL-DB-768x432.png 768w, https:\/\/devblogs.microsoft.com\/azure-sql\/wp-content\/uploads\/sites\/56\/2025\/03\/Streamlit-app-with-Fabric-SQL-DB-1536x864.png 1536w, https:\/\/devblogs.microsoft.com\/azure-sql\/wp-content\/uploads\/sites\/56\/2025\/03\/Streamlit-app-with-Fabric-SQL-DB.png 1920w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/a><figcaption id=\"figcaption_attachment_4397\" class=\"wp-caption-text\">Streamlit app with Fabric SQL DB<\/figcaption><\/figure><\/p>\n<p>&nbsp;<\/p>\n<h1>Conclusion<\/h1>\n<p>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. \u00a0To make this production ready and deploy to Azure web app , see <a href=\"https:\/\/techcommunity.microsoft.com\/blog\/appsonazureblog\/deploy-streamlit-on-azure-web-app\/4276108\">how to deploy Streamlit app to Azure web apps.<\/a><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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. \u00a0Streamlit 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 [&hellip;]<\/p>\n","protected":false},"author":174563,"featured_media":4404,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1,656,411],"tags":[],"class_list":["post-4361","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-azure-sql","category-fabric-sql-database","category-python"],"acf":[],"blog_post_summary":"<p>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. \u00a0Streamlit 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 [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/azure-sql\/wp-json\/wp\/v2\/posts\/4361","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/azure-sql\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/azure-sql\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/azure-sql\/wp-json\/wp\/v2\/users\/174563"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/azure-sql\/wp-json\/wp\/v2\/comments?post=4361"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/azure-sql\/wp-json\/wp\/v2\/posts\/4361\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/azure-sql\/wp-json\/wp\/v2\/media\/4404"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/azure-sql\/wp-json\/wp\/v2\/media?parent=4361"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/azure-sql\/wp-json\/wp\/v2\/categories?post=4361"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/azure-sql\/wp-json\/wp\/v2\/tags?post=4361"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}