Last active
April 27, 2024 01:28
-
-
Save shadiakiki1986/58f8af15fbc01ac153a58a73821893fe to your computer and use it in GitHub Desktop.
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 https://stackoverflow.com/a/63784887/4126114 | |
Usage: | |
In jupyter cell: | |
some_variable = "text" | |
%%writetemplate filename.txt | |
test: {some_variable} | |
""" | |
from IPython.core.magic import register_line_cell_magic | |
@register_line_cell_magic | |
def writetemplate(line, cell): | |
with open(line, 'w') as f: | |
f.write(cell.format(**globals())) |
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
"""Q. create file=ssh-key from var=kaggle-user-secret""" | |
from kaggle_secrets import UserSecretsClient | |
from pathlib import Path | |
def write_ssh_key_from_user_secret(secret_name): | |
user_secrets = UserSecretsClient() | |
gh_key = user_secrets.get_secret(secret_name) | |
path_key = Path.home()/".ssh"/"id_rsa" | |
path_key.parent.mkdir(exist_ok=True) | |
with open(path_key, "w") as f: | |
# Note: important to have newline at the end | |
f.write(f"""-----BEGIN OPENSSH PRIVATE KEY----- | |
{gh_key} | |
-----END OPENSSH PRIVATE KEY----- | |
""") | |
path_key.chmod(0o600) | |
def write_ssh_key_from_user_secret__github(): | |
write_ssh_key_from_user_secret("gh_key") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment