import requests from flask import Flask, Response, request import socket from pathlib import Path import streamlit from streamlit import config as _config from streamlit import web import kangas as kg import subprocess proj_dir = Path(__file__).parent filename = proj_dir / "streamlitapp.py" _config.set_option("server.headless", True) _config.set_option("server.port", 7840) args = [] hostname = socket.gethostname() ip_address = socket.gethostbyname(hostname) application = Flask(__name__, static_url_path='') def _is_running(kangas=False, streamlit=False): output = subprocess.check_output(['ps', '-A']).decode('utf-8') application.logger.info(f"{output}") running = False if kangas is True: if "kangas" in output: running = True application.logger.info(f"kangas is running") if streamlit is True: if "streamlit" in output: running = True application.logger.info(f"streamlit is running") else: running = False return running @application.route('/') def streamlit(): streamlit_runs = _is_running(False, True) if streamlit_runs is False: application.logger.info("Streamlit is not running yet") #subprocess.call(["streamlit", "run", "streamlitapp.py", "--server.port", "7840"]) resp = requests.get(f'http://{ip_address}:7840/') excluded_headers = ['content-encoding', 'content-length', 'transfer-encoding', 'connection'] headers = [(name, value) for (name, value) in resp.raw.headers.items() if name.lower() not in excluded_headers] response = Response(resp.content, resp.status_code, headers) application.logger.info(response) return response @application.route('/static/') def streamlit_static(path): resp = requests.get(f'http://{ip_address}:7840/static/{path}') excluded_headers = ['content-encoding', 'content-length', 'transfer-encoding', 'connection'] headers = [(name, value) for (name, value) in resp.raw.headers.items() if name.lower() not in excluded_headers] response = Response(resp.content, resp.status_code, headers) application.logger.info(response) return response @application.route('/vendor/') def streamlit_vendor(path): resp = requests.get(f'http://{ip_address}:7840/vendor/{path}') excluded_headers = ['content-encoding', 'content-length', 'transfer-encoding', 'connection'] headers = [(name, value) for (name, value) in resp.raw.headers.items() if name.lower() not in excluded_headers] response = Response(resp.content, resp.status_code, headers) application.logger.info(response) return response @application.route('/_stcore/') def streamlit_stcore(path): resp = requests.get(f'http://{ip_address}:7840/_stcore/{path}') excluded_headers = ['content-encoding', 'content-length', 'transfer-encoding', 'connection'] headers = [(name, value) for (name, value) in resp.raw.headers.items() if name.lower() not in excluded_headers] response = Response(resp.content, resp.status_code, headers) application.logger.info(response) return response if __name__ == "__main__": streamlit_runs = _is_running(False, True) if streamlit_runs is False: application.logger.info("Streamlit is not running yet") subprocess.Popen(["streamlit", "run", "/app/streamlitapp.py", "--server.port", "7840", "--server.headless", "true"]) application.run(host="0.0.0.0", port="7860", )