Skip to content
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

More flexible canister url script #4

Merged
merged 3 commits into from
Feb 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@ If prompted, proceed by installing the recommended plugins for VS Code.
### Running your Project

After the IDE has opened, run `dfx deploy` in the terminal to deploy the frontend and backend.
Click on the first green link at the end of the output to see your canister's frontend in the browser.
**NOTE**: If the printed link does not work when developing remotely (in a web browser), run `./scripts/canister_url.sh` and click the link that is shown there.
Click on the first green link at the end of the output to see your canister's frontend in the browser.
To interact with the backend canister, click on the second green link.
**NOTE**: When developing in GitHub Codespaces, run `./scripts/canister_urls.py` and use the links that are shown there.

For interactive development of the frontend canister, you can also start a node server by running `npm start`.
You can find your canister's frontend running under http://localhost:8080.
Expand Down
30 changes: 0 additions & 30 deletions scripts/canister_url.sh

This file was deleted.

45 changes: 45 additions & 0 deletions scripts/canister_urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#!/usr/bin/python3

import json
from os import environ, path

GREEN='\033[1;32m'
NORMAL='\033[0m'
BOLD='\033[1m'

def get_host():
if 'GITHUB_CODESPACES_PORT_FORWARDING_DOMAIN' in environ:
return f"https://{environ['CODESPACE_NAME']}-4943.{environ['GITHUB_CODESPACES_PORT_FORWARDING_DOMAIN']}"
else:
return 'http://127.0.0.1:4943'

if __name__ == "__main__":
root_path = path.dirname(path.realpath(__file__))
canister_id_file = f'{root_path}/../.dfx/local/canister_ids.json';
if not path.exists(canister_id_file):
print(f"Run 'dfx deploy' first.")
exit(1)
with open(canister_id_file) as f:
data = json.load(f)
frontend_canisters = []
backend_canisters = []

for key, value in data.items():
if key == '__Candid_UI':
candid_ui_canister_id = value['local']
elif 'frontend' in key:
frontend_canisters.append((key, value['local']))
else:
backend_canisters.append((key, value['local']))

host = get_host()

print(f"{BOLD}URLs:{NORMAL}")
if frontend_canisters:
print(f"""{BOLD} Frontend canister via browser{NORMAL}""")
for name, id in frontend_canisters:
print(f"{BOLD} {name}: {GREEN}{host}/?canisterId={id}{NORMAL}")
if backend_canisters:
print(f"{BOLD} Backend canister via Candid interface:")
for name, id in backend_canisters:
print(f"{BOLD} {name}: {GREEN}{host}/?canisterId={candid_ui_canister_id}&id={id}{NORMAL}")
Loading