You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

57 lines
1.3 KiB

  1. import streamlit as st
  2. import os
  3. import sys
  4. import importlib.util
  5. # Parse command-line arguments.
  6. if len(sys.argv) > 1:
  7. folder = os.path.abspath(sys.argv[1])
  8. else:
  9. folder = os.path.abspath(os.getcwd())
  10. # Get filenames for all files in this path, excluding this script.
  11. this_file = os.path.abspath(__file__)
  12. fnames = []
  13. for basename in os.listdir(folder):
  14. fname = os.path.join(folder, basename)
  15. if fname.endswith(".py") and fname != this_file:
  16. fnames.append(fname)
  17. # Make a UI to run different files.
  18. def format_func(s):
  19. els = s.split("/")[-1].split(".")[0].split("_")
  20. return " ".join(el for el in els).capitalize()
  21. fname_to_run = st.sidebar.selectbox(
  22. "Select an app", fnames, format_func=format_func
  23. )
  24. # Create module from filepath and put in sys.modules, so Streamlit knows
  25. # to watch it for changes.
  26. fake_module_count = 0
  27. def load_module(filepath):
  28. global fake_module_count
  29. modulename = "_dont_care_%s" % fake_module_count
  30. spec = importlib.util.spec_from_file_location(modulename, filepath)
  31. module = importlib.util.module_from_spec(spec)
  32. sys.modules[modulename] = module
  33. fake_module_count += 1
  34. # Run the selected file.
  35. with open(fname_to_run) as f:
  36. load_module(fname_to_run)
  37. filebody = f.read()
  38. exec(filebody, {})