Skip to content

Instantly share code, notes, and snippets.

@shadiakiki1986
Last active April 27, 2024 01:28
Show Gist options
  • Save shadiakiki1986/58f8af15fbc01ac153a58a73821893fe to your computer and use it in GitHub Desktop.
Save shadiakiki1986/58f8af15fbc01ac153a58a73821893fe to your computer and use it in GitHub Desktop.
"""
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()))
"""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