-
Notifications
You must be signed in to change notification settings - Fork 0
MacOS prototype #72
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
MacOS prototype #72
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
031e69e
pages/Code.py: remove unused import
mrjbq7 59b65ba
secure_ai: some cleanup, and check for both ~/.secure_ai and ~/SecureAI
mrjbq7 a5ba1a7
poetry: update dependencies
mrjbq7 d1a3534
secure_ai/Docs.py: fix old_files exception:
mrjbq7 1359481
secure_ai: prototype of a macos menubar app
mrjbq7 8bfb1e1
secure_ai: switch to using background thread for streamlit
mrjbq7 78981ff
secure_ai: use new event loop in background thread, check for port us…
mrjbq7 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or 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
Binary file not shown.
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,34 +1,91 @@ | ||
| import asyncio | ||
| import atexit | ||
| import os | ||
| import os.path | ||
| import pathlib | ||
| import platform | ||
| from streamlit import config as _config | ||
| import streamlit.web.bootstrap as bootstrap | ||
| import signal | ||
| import sys | ||
| import threading | ||
| import webbrowser | ||
|
|
||
| HERE = pathlib.Path(__file__).parent | ||
| from streamlit import config | ||
| from streamlit.web import bootstrap | ||
|
|
||
|
|
||
| def app(): | ||
| system_name = platform.system() | ||
|
|
||
| if system_name == "Darwin": | ||
| headless = False | ||
| else: | ||
| headless = True | ||
|
|
||
| # Turn off file change monitoring and run in headless mode | ||
| _config.set_option("server.port", 8501) | ||
| _config.set_option("server.address", "0.0.0.0") | ||
| _config.set_option("server.headless", headless) | ||
| _config.set_option("server.fileWatcherType", None) | ||
| _config.set_option("browser.gatherUsageStats", False) | ||
| _config.set_option("client.toolbarMode", "minimal") | ||
| config.set_option("server.port", 8501) | ||
| config.set_option("server.address", "0.0.0.0") | ||
| config.set_option("server.headless", True) | ||
| config.set_option("server.fileWatcherType", None) | ||
| config.set_option("browser.gatherUsageStats", False) | ||
| config.set_option("client.toolbarMode", "minimal") | ||
|
|
||
| root = pathlib.Path(__file__).parent | ||
|
|
||
| bootstrap.run( | ||
| str(HERE.joinpath("Docs.py")), | ||
| str(root.joinpath("Docs.py")), | ||
| command_line=None, | ||
| args=list(), | ||
| flag_options=dict(), | ||
| ) | ||
|
|
||
| def mac_app(): | ||
| # run app() in a background thread | ||
| bootstrap._set_up_signal_handler = lambda x: None | ||
| def loop_in_thread(): | ||
| loop = asyncio.new_event_loop() | ||
| asyncio.set_event_loop(loop) | ||
| loop.run_until_complete(app()) | ||
| t = threading.Thread(target=loop_in_thread, daemon=True) | ||
| t.start() | ||
|
|
||
| version = 'SecureAI' | ||
| icon = None | ||
|
|
||
| # look for the contents directory of the app bundle | ||
| contents_path = os.path.dirname(os.path.dirname(sys.executable)) | ||
|
|
||
| # parse the version from the Info.plist | ||
| info_plist_path = os.path.join(contents_path, 'Info.plist') | ||
| if os.path.exists(info_plist_path): | ||
| with open(info_plist_path, 'rb') as f: | ||
| import plistlib | ||
| try: | ||
| load_plist = plistlib.load | ||
| except AttributeError: | ||
| load_plist = plistlib.readPlist | ||
| plist = load_plist(f) | ||
| version = plist['CFBundleGetInfoString'] | ||
|
|
||
| # find the menubar icon | ||
| icon_paths = [os.path.join(contents_path, 'Resources/menubar.ico'), | ||
| 'resources/menubar.ico'] | ||
| for icon_path in icon_paths: | ||
| if os.path.exists(icon_path): | ||
| icon = icon_path | ||
|
|
||
| # configure and run the menubar app | ||
| import rumps | ||
| mac = rumps.App("SecureAI", icon=icon) | ||
| mac.menu = [ | ||
| rumps.MenuItem(version), | ||
| None, | ||
| rumps.MenuItem("Open", callback=lambda x: webbrowser.open("http://localhost:8501")), | ||
| None, | ||
| ] | ||
| mac.run() | ||
|
|
||
| def check_port_in_use(): | ||
| import socket | ||
| with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: | ||
| if not s.connect_ex(('localhost', 8501)): | ||
| print("ERROR: Port 8501 is already in use!") | ||
| sys.exit(1) | ||
|
|
||
| if __name__ == "__main__": | ||
| app() | ||
| check_port_in_use() | ||
| if sys.platform == 'darwin': | ||
| mac_app() | ||
| else: | ||
| app() |
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Haha both of these are great changes and obvious from looking from the outside.