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
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workkloads.

Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Deploying An Image Captioning Server With BentoML
  • Accelerating AI Inference With TensorRT
  • AI's Dilemma: When to Retrain and When to Unlearn?
  • Getting Started With GenAI on BigQuery: A Step-by-Step Guide

Trending

  • Key Considerations in Cross-Model Migration
  • Transforming AI-Driven Data Analytics with DeepSeek: A New Era of Intelligent Insights
  • Rust and WebAssembly: Unlocking High-Performance Web Apps
  • Debugging Core Dump Files on Linux - A Detailed Guide
  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 user avatar
Akmal Chaudhri
DZone Core CORE ·
Feb. 04, 22 · Tutorial
Likes (3)
Comment
Save
Tweet
Share
4.6K Views

Join the DZone community and get the full member experience.

Join For Free

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
  • Accelerating AI Inference With TensorRT
  • AI's Dilemma: When to Retrain and When to Unlearn?
  • Getting Started With GenAI on BigQuery: A Step-by-Step Guide

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

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 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!