Open URL using button and add plots to new URL

Summary

I need to open a given url after pressing button and add plots to new URL (ex. http://localhost:port). I am able to open given URL using button, but not able to add a plot to given URL, plot occurs on main page only instead of new page

Steps to reproduce

Code snippet:

import webbrowser
import matplotlib.pyplot as plt
import numpy as np
import streamlit as st

total_points = st.number_input('Choose number of points', min_value=2)
xpoints = np.arange(total_points)
ypoints = xpoints ** 2
fig = plt.figure(figsize=(5, 4))
ax = fig.add_axes([0.1, 0.1, 0.8, 0.8])

if st.button('With Line'):
    url = f'http://127.0.0.1:{8000}/'
    webbrowser.open_new_tab(url)
    ax.plot(xpoints, ypoints)
    st.pyplot(fig)

if st.button('Without Line'):
    url = f'http://127.0.0.1:{8001}/'
    webbrowser.open_new_tab(url)
    ax.plot(xpoints, ypoints, 'o')
    st.pyplot(fig)

If applicable, please provide the steps we should take to reproduce the error or specified behavior.

Expected behavior:

I am expecting that if I press any button, new tab should be open(with given URL) in same browser and show me required plot on new tab.

Actual behavior:

I am able to open new tab after pressing button, but for plotting main page is currently used.

Debug info

  • Streamlit version: 1.20.0
  • Python version: 3.9
  • Using Conda
  • OS version: Windows 11
  • Browser version: Google crome (Version 111.0.5563.148)

Requirements file

Using Conda
matplotlib==3.7.1
numpy==1.22.4
streamlit==1.20.0

If you’re trying to use streamlit on a different page, I would suggest creating a multipage app (Create a multipage app - Streamlit Docs), and making the other urls into actual streamlit pages, and then doing the plotting on those pages.

If you need to be able to pass information along the other pages, you can either do this by adding variables to session state Session State - Streamlit Docs or by using query params st.experimental_get_query_params - Streamlit Docs

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.