Open URL using button and add plots to new URL


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'{8000}/'
    ax.plot(xpoints, ypoints)

if st.button('Without Line'):
    url = f'{8001}/'
    ax.plot(xpoints, ypoints, 'o')

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

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.