Methods for using Git with Google Colab

前端 未结 12 805
臣服心动
臣服心动 2020-12-12 11:42

Are there any recommended methods to integrate git with colab?

For example, is it possible to work off code from google source repositories or the likes?

Nei

12条回答
  •  一向
    一向 (楼主)
    2020-12-12 12:04

    This works if you want to share your repo and colab. Also works if you have multiple repos. Just throw it in a cell.

    import ipywidgets as widgets
    from IPython.display import display
    import subprocess
    
    class credentials_input():
        def __init__(self, repo_name):
            self.repo_name = repo_name
            self.username = widgets.Text(description='Username', value='')
            self.pwd = widgets.Password(description = 'Password', placeholder='password here')
    
            self.username.on_submit(self.handle_submit_username)
            self.pwd.on_submit(self.handle_submit_pwd)        
            display(self.username)
    
        def handle_submit_username(self, text):
            display(self.pwd)
            return
    
        def handle_submit_pwd(self, text):
            cmd = f'git clone https://{self.username.value}:{self.pwd.value}@{self.repo_name}'
            process = subprocess.Popen(cmd.split(), stdout=subprocess.PIPE)
            output, error = process.communicate()
            print(output, error)
            self.username.value, self.pwd.value = '', ''
    
    get_creds = credentials_input('github.com/username/reponame.git')
    get_creds
    

提交回复
热议问题