Last active
October 10, 2019 03:03
-
-
Save virusvn/2a9f6266e5a33c8ddab8313e61b5e4f7 to your computer and use it in GitHub Desktop.
Run Streamlit apps inside other apps by remote script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from typing import List, Dict | |
import logging | |
import json | |
import urllib.request | |
import streamlit as st | |
# Get an instance of a logger | |
logger = logging.getLogger(__name__) | |
# Change this json to your file | |
JSON_URL = "https://fullstackstation.com/streamlit_apps.json" | |
def main(): | |
apps = fetch_json(JSON_URL) # type: List[Dict[str, str]] | |
logger.info(apps) | |
app_names = [] | |
for app in apps: | |
if app["url"].endswith(".py"): | |
app_names.append(app["name"]) | |
run_app = st.sidebar.selectbox("Select the component", app_names) | |
# Fetch the content | |
for app in apps: | |
if app["name"] == run_app: | |
python_code = fetch_python_code(app["url"]) | |
# Run the child app | |
if python_code is not None: | |
try: | |
exec(python_code) | |
st.code(python_code) | |
except Exception as e: | |
st.write("Error occurred when execute [{0}]".format(run_app)) | |
st.error(str(e)) | |
logger.error(e) | |
@st.cache | |
def fetch_json(url: str): | |
data = urllib.request.urlopen(url).read() | |
output = json.loads(data) | |
return output | |
@st.cache | |
def fetch_python_code(url: str): | |
data = urllib.request.urlopen(url).read() | |
return data.decode("utf-8") | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment