kangas-demo / app.py
CalebCometML's picture
Update app.py
53f3300
raw
history blame
4.04 kB
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__)
@application.route('/kangas', strict_slashes=False)
@application.route('/kangas/<path:path>')
def kangas_proxy(path=None):
if kg._is_running("node", "kangas"):
application.logger.info("Kangas is running on fetch")
else:
application.logger.info("Kangas is not running on fetch")
application.logger.info("Trying to launch Kangas now")
subprocess.call(["kangas", "server", "--frontend-port", "7640", "--frontend-root", "kangas"])
#kg.launch(host=ip_address, port=7640, debug="DEBUG")
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():
web.bootstrap.run(str(filename), "", args, "")
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
if __name__ == "__main__":
kg.launch(host=ip_address, port=7640, debug="DEBUG")
#subprocess.call(["kangas", "server", "--frontend-port=7640"])
# subprocess.call(['ps'])
if kg._is_running("node", "kangas"):
application.logger.info("Kangas is running on startup")
else:
application.logger.info("Kangas is not running on startup")
#application.run(debug_level="DEBUG")