diff --git a/README.md b/README.md index 3626851..14480b6 100644 --- a/README.md +++ b/README.md @@ -36,7 +36,9 @@ Obtain your `client_id` and `client_secret` from Schwab, then save to `auth/clie } ``` -### 2. Fetch Data and Generate Chart +### 2. Start the Main Loop and Dashboard + +Run the main loop, which will fetch data and automatically launch the Streamlit dashboard: ```bash python main.py @@ -44,17 +46,21 @@ python main.py This will: - Authenticate with Schwab (browser login on first run) -- Fetch account positions and option prices +- Fetch account positions and option prices on each iteration - Generate `data/gantt.png` showing position timelines - Cache data locally in `data/positions.json` and `data/tracking.json` +- **Launch the Streamlit dashboard at `http://localhost:8501` after the first data fetch** +- Refresh data every 60 seconds; dashboard reflects updates on page reload + +### Running the Dashboard Standalone -### 3. View the Dashboard +If you want to run the dashboard independently: ```bash -streamlit run streamlit_app.py +streamlit run dash.py ``` -Opens an interactive dashboard at `http://localhost:8501` showing the Gantt chart and values table. +This reads cached data from the `data/` folder (generated by `main.py`). ## Testing diff --git a/streamlit_app.py b/dash.py similarity index 100% rename from streamlit_app.py rename to dash.py diff --git a/main.py b/main.py index e952b0f..283b730 100644 --- a/main.py +++ b/main.py @@ -12,6 +12,8 @@ # See the License for the specific language governing permissions and # limitations under the License. +import subprocess +import sys import time import auth @@ -25,6 +27,8 @@ def main() -> None: tokens = auth.get_or_create_tokens() iteration = 0 + streamlit_process = None + try: while True: # Check if token needs refreshing @@ -44,10 +48,30 @@ def main() -> None: data.update_tracking() plot.make_gantt_chart() + # Launch Streamlit dashboard after first iteration + if iteration == 0: + print("Launching Streamlit dashboard (dash.py)...") + try: + streamlit_process = subprocess.Popen( + [sys.executable, "-m", "streamlit", "run", "dash.py", "--logger.level=warning"], + stdout=subprocess.DEVNULL, + stderr=subprocess.DEVNULL, + ) + print("Dashboard running at http://localhost:8501") + except Exception as exc: + print(f"Failed to launch Streamlit: {exc}") + iteration += 1 time.sleep(60) except KeyboardInterrupt: - print("Stopped by user.") + print("\nStopped by user.") + if streamlit_process: + print("Shutting down Streamlit dashboard...") + streamlit_process.terminate() + try: + streamlit_process.wait(timeout=5) + except subprocess.TimeoutExpired: + streamlit_process.kill() if __name__ == "__main__":