New to streamlit and flask. I am trying to create a simple page containing links. When these links are clicked, I want the connected files to run in the new window or tab. Here is my code that is trying to achieve this. But the new link does not show anything. Also, the port keeps on changing.
main.py
<code>import streamlit as st
import subprocess
from flask import Flask, Response
import threading
# Define a dictionary mapping filenames to Python scripts
scripts = {
"Script 1": "script1.py",
"Script 2": "script2.py",
# Add more scripts as needed
}
# Route for running Script 1
app_script1 = Flask(__name__)
@app_script1.route("/run_script1")
def run_script1():
# Use subprocess to run the Streamlit app for Script 1
subprocess.Popen(["streamlit", "run", "script1.py"])
return Response(status=204)
def start_flask_app_script1():
app_script1.run(port=8503)
# Route for running Script 2
app_script2 = Flask(__name__)
@app_script2.route("/run_script2")
def run_script2():
# Use subprocess to run the Streamlit app for Script 2
subprocess.Popen(["streamlit", "run", "script2.py"])
return Response(status=204)
def start_flask_app_script2():
app_script2.run(port=8504)
# Start the Flask app for Script 2 in a separate thread
threading.Thread(target=start_flask_app_script2).start()
# Display links for running each script
for script_name in scripts:
if st.button(script_name):
if script_name == "Script 1":
# Start the Flask app for Script 1 in a separate thread
threading.Thread(target=start_flask_app_script1).start()
script_url = "http://localhost:8503"
elif script_name == "Script 2":
threading.Thread(target=start_flask_app_script2).start()
script_url = "http://localhost:8504"
st.markdown(f'<a href="{script_url}" target="_blank">{script_name}</a>', unsafe_allow_html=True)
</code>
<code>import streamlit as st
import subprocess
from flask import Flask, Response
import threading
# Define a dictionary mapping filenames to Python scripts
scripts = {
"Script 1": "script1.py",
"Script 2": "script2.py",
# Add more scripts as needed
}
# Route for running Script 1
app_script1 = Flask(__name__)
@app_script1.route("/run_script1")
def run_script1():
# Use subprocess to run the Streamlit app for Script 1
subprocess.Popen(["streamlit", "run", "script1.py"])
return Response(status=204)
def start_flask_app_script1():
app_script1.run(port=8503)
# Route for running Script 2
app_script2 = Flask(__name__)
@app_script2.route("/run_script2")
def run_script2():
# Use subprocess to run the Streamlit app for Script 2
subprocess.Popen(["streamlit", "run", "script2.py"])
return Response(status=204)
def start_flask_app_script2():
app_script2.run(port=8504)
# Start the Flask app for Script 2 in a separate thread
threading.Thread(target=start_flask_app_script2).start()
# Display links for running each script
for script_name in scripts:
if st.button(script_name):
if script_name == "Script 1":
# Start the Flask app for Script 1 in a separate thread
threading.Thread(target=start_flask_app_script1).start()
script_url = "http://localhost:8503"
elif script_name == "Script 2":
threading.Thread(target=start_flask_app_script2).start()
script_url = "http://localhost:8504"
st.markdown(f'<a href="{script_url}" target="_blank">{script_name}</a>', unsafe_allow_html=True)
</code>
import streamlit as st
import subprocess
from flask import Flask, Response
import threading
# Define a dictionary mapping filenames to Python scripts
scripts = {
"Script 1": "script1.py",
"Script 2": "script2.py",
# Add more scripts as needed
}
# Route for running Script 1
app_script1 = Flask(__name__)
@app_script1.route("/run_script1")
def run_script1():
# Use subprocess to run the Streamlit app for Script 1
subprocess.Popen(["streamlit", "run", "script1.py"])
return Response(status=204)
def start_flask_app_script1():
app_script1.run(port=8503)
# Route for running Script 2
app_script2 = Flask(__name__)
@app_script2.route("/run_script2")
def run_script2():
# Use subprocess to run the Streamlit app for Script 2
subprocess.Popen(["streamlit", "run", "script2.py"])
return Response(status=204)
def start_flask_app_script2():
app_script2.run(port=8504)
# Start the Flask app for Script 2 in a separate thread
threading.Thread(target=start_flask_app_script2).start()
# Display links for running each script
for script_name in scripts:
if st.button(script_name):
if script_name == "Script 1":
# Start the Flask app for Script 1 in a separate thread
threading.Thread(target=start_flask_app_script1).start()
script_url = "http://localhost:8503"
elif script_name == "Script 2":
threading.Thread(target=start_flask_app_script2).start()
script_url = "http://localhost:8504"
st.markdown(f'<a href="{script_url}" target="_blank">{script_name}</a>', unsafe_allow_html=True)
script1.py1
<code>import streamlit as st
st.write("Inside script1.py")
</code>
<code>import streamlit as st
st.write("Inside script1.py")
</code>
import streamlit as st
st.write("Inside script1.py")
script1.py1
<code>import streamlit as st
st.write("Inside script2.py")
</code>
<code>import streamlit as st
st.write("Inside script2.py")
</code>
import streamlit as st
st.write("Inside script2.py")