DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Deploying An Image Captioning Server With BentoML
  • Detecting Duplicates With Machine Learning in 2026: From String Metrics to Embeddings and LLMs
  • Building and Serving a Custom Model With Azure ML, Then Wiring It Into a Foundry Agent
  • How Multi-Agent Systems can replace most of Manual ML Validation decisions - The Karpathy Loop Approach

Trending

  • OwlVigil Open-Sources Its Go SDK for Enterprise AI
  • Best of Three Worlds: AI Agents Writing In Rust and Java Beat the C Library
  • 7 Swift Features That Will Let You Delete Code Tomorrow
  • Controller and Data Access: Understanding Two-Layer Architecture Through Abstraction and Encapsulation

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  1. DZone
  2. Data Engineering
  3. AI/ML
  4. Connect Streamlit to SingleStore

Connect Streamlit to SingleStore

Get great visualizations in minutes! This article will show how SingleStore can be quickly used with an open-source application framework called Streamlit.

By 
Akmal Chaudhri
DZone Core CORE ·
Feb. 04, 22 · Tutorial
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
5.0K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

Abstract

This article will show how SingleStore can be quickly used with Streamlit, an open-source application framework for Machine Learning and Data Science.

Introduction

The Streamlit documentation describes how to connect Streamlit to MySQL. It is trivial to adapt the code to work with SingleStore. Let's see how.

To begin with, we need to create a free Managed Service account on the SingleStore website. At the time of writing, the Managed Service account from SingleStore comes with $500 of Credits. This is more than adequate for the case study described in this article.

Create the Database Table

In our SingleStore Managed Service account, let's use the SQL Editor to create a new database and table. We'll use the example provided by Streamlit:

SQL
 
CREATE DATABASE pets;

USE pets;

CREATE TABLE mytable (
     name VARCHAR(80),
     pet VARCHAR(80)
);

INSERT INTO mytable VALUES
('Mary', 'dog'),
('John', 'cat'),
('Robert', 'bird');


Install Streamlit

We'll install Streamlit, as follows:

Shell
 
pip install streamlit


The Streamlit example uses MySQL Connector. However, for most Python use cases, SingleStore recommends using PyMySQL. All we need to do is install this, as follows:

Shell
 
pip install PyMySQL


Modify the Example Application

In the file streamlit_app.py we need to make a few minor modifications:

  • Replace import mysql.connector with import pymysql
  • Replace mysql.connector.connect with pymysql.connect
  • Replace st.secrets["mysql"] with st.secrets["singlestore"]
  • Remove references to st.cache

Here is the complete modified code listing:

Python
 
# streamlit_app.py

import streamlit as st
import pymysql

# Initialize connection.

def init_connection():
    return pymysql.connect(**st.secrets["singlestore"])
  
conn = init_connection()

# Perform query.

def run_query(query):
    with conn.cursor() as cur:
        cur.execute(query)
        return cur.fetchall()
      
rows = run_query("SELECT * FROM mytable;")

# Print results.

for row in rows:
    st.write(f"{row[0]} has a :{row[1]}:")


Create a Secrets File

Our local Streamlit application will read secrets from a file .streamlit/secrets.toml in our application's root directory. We need to create this file, as follows:

Plain Text
 
# .streamlit/secrets.toml

[singlestore]
host = "<TO DO>"
port = 3306
database = "pets"
user = "admin"
password = "<TO DO>"


The <TO DO> for host and password should be replaced with the values obtained from the SingleStore Managed Service when creating a cluster.

Run the Code

We can run the Streamlit application as follows:

Shell
 
streamlit run streamlit_app.py


The output in a web browser should look like Figure 1.

Figure 1. Streamlit Output.

Conclusion

This article showed rapid code modifications to the Streamlit MySQL example so that Streamlit could be used with SingleStore instead. We’ll explore Streamlit further in future articles and use its capabilities in greater depth. Stay tuned!

Machine learning Application framework

Published at DZone with permission of Akmal Chaudhri. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Deploying An Image Captioning Server With BentoML
  • Detecting Duplicates With Machine Learning in 2026: From String Metrics to Embeddings and LLMs
  • Building and Serving a Custom Model With Azure ML, Then Wiring It Into a Foundry Agent
  • How Multi-Agent Systems can replace most of Manual ML Validation decisions - The Karpathy Loop Approach

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook