Terminating python script after plotting

Summary

Iโ€™m plotting stock history for various tickers which streamlit does with no issues. However after the plotting completes, I want the python script to be unblocked. The only way to terminate is to use Ctrl-C but this is manual intervention. Iโ€™m calling the python script from a GUI so the gui is hung until doing ctrl-C.

Steps to reproduce

Code snippet:

# This is the GUI script which includes the method 'plot_history_clicked' which calls the command to run streamlit

import pathlib
import pygubu
import quote_hist
import quotes
import ticker
import os

PROJECT_PATH = pathlib.Path(__file__).parent
PROJECT_UI = PROJECT_PATH / "main_gui.ui"

class QuoteApp:
    def __init__(self, master=None):
        self.builder = builder = pygubu.Builder()
        builder.add_resource_path(PROJECT_PATH)
        builder.add_from_file(PROJECT_UI)
        # Main widget
        self.mainwindow = builder.get_object("main_window", master)
        # self.text_box = builder.get_object("text_box")
        builder.connect_callbacks(self)

    def run(self):
        self.mainwindow.mainloop()

    def get_quote_history(self):
        quote_hist.main()

    def get_quotes(self):
        quotes.main()
        
    def get_ticker_clicked(self):
        ticker_entry = self.builder.get_object("user_ticker_entry")
        ticker_sym = ticker_entry.get().lower()
        ticker.main(ticker_sym)
        
    def plot_history_clicked(self):
        os.system('streamlit run plot_data_streamlit.py')
        
    def run(self):
        self.mainwindow.mainloop()
        
    def close_window(self):
        self.mainwindow.destroy()
# This is the function in the module 'plot_data_streamlit.py' which plots the data:

def plot_ticker_hist(data: List[Tuple[str, DataFrame]]):
    """plot figures from list of ticker dataframe tuples.
    Args:
        data (list[Tuple]): list of (ticker, ticker dataframes)
    """
        
    for tupl in data:
        
        df = tupl[1]
        df[['Open','Close']].astype('float')
        df['Date'].astype('datetime64') # date column from dbase is str type
        df.sort_values(by='Date', inplace=True) # database not likely in sorted order
        df = df[['Date','Open', 'Close']] # reduce number of columns
        df = df.melt('Date', var_name='category', value_name='price')
        
        st.markdown(f"""
        Shown is the **opening** and **closing** price of **{tupl[0].upper()}**
        """)
        
        chart = alt.Chart(df).mark_line().encode(x=alt.X('Date:T'), y=alt.Y('price:Q'), color=alt.Color("category:N"))
        st.altair_chart(chart, use_container_width=True)

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

Expected behavior:

At the end of the for loop, I would like to have a command which terminates or unblocks the event loop so that python scripts completes and therefore unblocks the main gui.

Actual behavior:

As noted, after streamlit plots the data to the browser, the script remains blocked and therefore blocks the GUI.

Debug info

  • Streamlit version: 1.14.0
  • Python version: 3.10
  • Using PyEnv
  • OS version: Windows 10
  • Browser version: Edge 107.0.1418.62

Requirements file

[tool. Poetry]
name = โ€œget-stocksโ€
version = โ€œ0.1.0โ€
description = โ€œโ€
authors = [โ€œxxxxxโ€]
readme = โ€œREADME.mdโ€
packages = [{include = โ€œget_stocksโ€}]

[tool.poetry.dependencies]
python = โ€œ~3.10โ€
pandas = โ€œ^1.5.1โ€
pandas-datareader = โ€œ^0.10.0โ€
beautifulsoup4 = โ€œ^4.11.1โ€
streamlit = โ€œ^1.14.0โ€
pygubu = โ€œ^0.27โ€

Links

  • Link to your GitHub repo:
  • Link to your deployed app:

Additional information

If needed, add any other context about the problem here.

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