DZone
Big Data Zone
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
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Big Data Zone > 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.

Akmal Chaudhri user avatar by
Akmal Chaudhri
CORE ·
Feb. 04, 22 · Big Data Zone · Tutorial
Like (3)
Save
Tweet
3.81K 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.

Popular on DZone

  • Unified Observability Exporters: Metrics, Logs, and Tracing
  • Making Your Own Express Middleware
  • Understand Source Code — Deep Into the Codebase, Locally and in Production
  • Legacy Modernization and Hybrid Cloud with Kafka in Healthcare

Comments

Big Data Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

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

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo