Spaces:
Running
Running
File size: 5,746 Bytes
95dc95a 1184b99 95dc95a 1184b99 bbcff30 1184b99 53f3300 1184b99 e97324d 1184b99 c7635ba 1184b99 95dc95a d0ca0a2 19b6ea5 1184b99 962597f 06c9234 962597f 1184b99 a650a84 c7635ba 5faac1a a650a84 1184b99 1252e5d 607d978 6163597 1252e5d 607d978 19b6ea5 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
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('/kangas', strict_slashes=False)
@application.route('/kangas/<path:path>')
def kangas_proxy(path=None):
query_string = request.query_string.decode('utf-8')
if path is None and len(query_string) < 1:
resp = requests.get(f'http://{ip_address}:7640/')
else:
if path is None:
path = ''
resp = requests.get(f'http://{ip_address}:7640/{str(path)}?{query_string}')
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)
return response
@application.route('/api/<path:path>')
def api_proxy(path):
query_string = request.query_string.decode('utf-8')
resp = requests.get(f'http://{ip_address}:7640/api/{path}?{query_string}')
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)
return response
@application.route('/_next/<path:path>')
def next_proxy(path=None):
application.logger.info("PATH: %s" % path)
resp = requests.get(f'http://{ip_address}:7640/_next/%s' % 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)
return response
@application.route('/<path>.png')
@application.route('/<path>.ico')
def image_proxy(path=None):
application.logger.info(path)
resp = requests.get(f'http://{ip_address}:7640/%s.png' % 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('/')
def streamlit():
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/<path:path>')
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/<path:path>')
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/<path:path>')
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", "streamlitapp.py", "--server.port", "7840"])
if kg._is_running("node", "kangas"):
application.logger.info("Kangas is running on startup")
else:
application.logger.info("Kangas is not running on startup")
subprocess.Popen(["kangas", "server", "--frontend-port", "7640", "--frontend-root", "kangas"])
application.run(host="0.0.0.0", port="7860")
|