Integrating Flask and Streamlit

A Guide to Creating Interactive Web Pages and Embedding Them Into Existing Websites

Introduction

Flask and Streamlit are two popular Python frameworks used for web development and data science. Flask is a lightweight web framework used to build web applications, while Streamlit is a high-level library used for building interactive data science web applications. In this article, we’ll explore how to integrate Flask and Streamlit to create a dynamic web page and how to add that web page onto an existing website.

Integrating Flask and Streamlit

The first step in integrating Flask and Streamlit is to create a Flask application that will act as the backend for our web page. We’ll use Flask to serve our Streamlit application, and the Streamlit application will be embedded in the Flask application.

To get started, create a new Flask application using the following code:

from flask import Flask, render_template
import streamlit as st

app = Flask(__name__)
@app.route('/')
def index():
    return render_template('index.html')
@app.route('/streamlit')
def streamlit():
    st.set_page_config(page_title="My Streamlit App")
    st.write("Hello, world!")
if __name__ == '__main__':
    app.run()

In the code above, we first import the necessary libraries: Flask, render_template, and Streamlit. We then create a new Flask application instance and define two routes: ‘/’ and ‘/streamlit’.

The ‘/’ route returns an HTML template called index.html. We haven’t created this file yet, but we’ll do that in the next step.

The ‘/streamlit’ route is where we’ll embed our Streamlit application. We set the page title using the set_page_config() method, and we write "Hello, world!" to the Streamlit app.

Creating the HTML template

Now we need to create an HTML template that our Flask application will use to render the web page. Create a new file called index.html in the templates folder with the following code:

<!doctype html>
<html>
  <head>
    <title>My Flask App</title>
  </head>
  <body>
    <h1>Welcome to my Flask app</h1>
    <iframe src="/streamlit" width="100%" height="800"></iframe>
  </body>
</html>

In the code above, we define the title of the web page and create an HTML iframe element that will display the Streamlit application. The iframe has a width of 100% and a height of 800 pixels, but you can adjust these values as needed.

Running the application

Now that we have everything set up, we can run the application using the following command:

python app.py

This will start the Flask server on your local machine. You can access the web page by opening your web browser and navigating to http://localhost:5000/.

Integrating with an existing website

To integrate this dynamic web page into an existing website, we need to create a new HTML page that includes the iframe element we created earlier. We can then link to this HTML page from the existing website.

Here’s an example of how to create a new HTML page that includes the iframe element:

<!doctype html>
<html>
  <head>
    <title>My Existing Website</title>
  </head>
  <body>
    <h1>Welcome to my existing website</h1>
    <iframe src="http://localhost:5000/" width="100%" height="800"></iframe>
  </body>
</html>

In the code above, we define the title of the existing website and include the iframe element we created earlier. We set the src attribute to the URL of our Flask application, which in this case is http://localhost:5000/.

We can now link to this HTML page from our existing website by creating a new link or button that points to the URL of our new HTML page. This will allow users to navigate to the dynamic web page we created using Flask and Streamlit.

Conclusion

In this article, we’ve explored how to integrate Flask and Streamlit to create a dynamic web page and how to add that web page onto an existing website. By combining these two powerful tools, we can create interactive data science web applications that can be easily embedded into any website.

Flask provides a lightweight web framework that is easy to use and highly customizable, while Streamlit provides a high-level library that makes it easy to create interactive data science web applications. Together, these tools make it easy to create dynamic web pages that can be embedded into any website.

If you’re interested in building dynamic web pages that include data visualizations, interactive widgets, and machine learning models, then Flask and Streamlit are definitely worth exploring. With these tools, you can easily create web applications that are both powerful and user-friendly.

Enjoyed this article?

Share it with your network to help others discover it

Continue Learning

Discover more articles on similar topics