Last active
November 23, 2020 18:04
-
-
Save HeinrichAD/705b4341097ab24895ff28349a6343b0 to your computer and use it in GitHub Desktop.
Little collaboration between Google Colab, Drive (cloud) and GitHub.
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
{ | |
"nbformat": 4, | |
"nbformat_minor": 0, | |
"metadata": { | |
"colab": { | |
"name": "colab-git-project.ipynb", | |
"provenance": [], | |
"collapsed_sections": [] | |
}, | |
"kernelspec": { | |
"name": "python3", | |
"display_name": "Python 3" | |
}, | |
"accelerator": "GPU" | |
}, | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "MH_9K6R-MmAL" | |
}, | |
"source": [ | |
"[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/gist/HeinrichAD/705b4341097ab24895ff28349a6343b0/colab_git_project.ipynb)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
}, | |
"id": "dd8P8m7wfxpt", | |
"outputId": "79545c2c-af76-45d5-84d1-3b0d15a350c9" | |
}, | |
"source": [ | |
"#@title Google Drive\n", | |
"\n", | |
"# git project directory, google drive root\n", | |
"PROJECT_PATH = \"/\" #@param {type:\"string\"}\n", | |
"ROOT = f\"/content/gdrive/My Drive/{PROJECT_PATH}\"\n", | |
"\n", | |
"# Reload all modules (except those excluded by %aimport)\n", | |
"# every time before executing the python code typed.\n", | |
"%load_ext autoreload\n", | |
"%autoreload 2\n", | |
"\n", | |
"# mount google drive\n", | |
"from google.colab import drive\n", | |
"drive.mount('/content/gdrive')\n", | |
"\n", | |
"# create project root directory if necessary\n", | |
"from os import path\n", | |
"if not path.isdir(ROOT):\n", | |
" !mkdir -p \"$ROOT\"\n", | |
"\n", | |
"# set git project directory as working directory\n", | |
"# and list \"local\" projects\n", | |
"%cd \"$ROOT\"\n", | |
"%ls -ls" | |
], | |
"execution_count": null, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": [ | |
"Drive already mounted at /content/gdrive; to attempt to forcibly remount, call drive.mount(\"/content/gdrive\", force_remount=True).\n", | |
"/content/gdrive/My Drive\n", | |
"total 4\n", | |
"4 drwx------ 2 root root 4096 Mar 28 2020 \u001b[0m\u001b[01;34m'Colab Notebooks'\u001b[0m/\n" | |
], | |
"name": "stdout" | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
}, | |
"id": "xwlCH4skGlvO", | |
"outputId": "888fce2e-364f-4bf0-cae0-2b8c46218746" | |
}, | |
"source": [ | |
"#@title GitHub\n", | |
"REPOSITORY = \"thdla/DLA2020\" #@param {type:\"string\"}\n", | |
"USERNAME = \"Google Colab\" #@param {type:\"string\"}\n", | |
"EMAIL = \"google@colab.fake\" #@param {type:\"string\"}\n", | |
"BRANCH = \"\" #@param {type:\"string\"}\n", | |
"TOKEN = \"\" #@param {type:\"string\"}\n", | |
"###################################################\n", | |
"# TOKEN: Only necessary for private repositories! #\n", | |
"# https://github.com/settings/tokens #\n", | |
"###################################################\n", | |
"\n", | |
"url = f\"https://{TOKEN}@github.com/{REPOSITORY}\"\n", | |
"\n", | |
"# set git configs if necessary\n", | |
"!git config --global user.name || git config --global user.name \"$USERNAME\"\n", | |
"!git config --global user.email || git config --global user.email \"$EMAIL\"\n", | |
"\n", | |
"# start in project directory\n", | |
"%cd \"$ROOT\"\n", | |
"\n", | |
"# get repository name from url\n", | |
"from urllib.parse import urlparse\n", | |
"repoName = urlparse(url).path.strip(\"/\").split('/')[1].split('.')[0]\n", | |
"print(repoName)\n", | |
"\n", | |
"# clone if necessary\n", | |
"if not path.isdir(repoName):\n", | |
" args = \"\" if not BRANCH else f\" --single-branch --branch {BRANCH} \"\n", | |
" !git clone --recurse-submodules $args \"$url\"\n", | |
"\n", | |
"# move to repository directory, (set branch,) fetch new data (pull)\n", | |
"%cd $repoName\n", | |
"if BRANCH:\n", | |
" !git checkout $BRANCH\n", | |
"!git pull\n", | |
"%ls -lah" | |
], | |
"execution_count": null, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": [ | |
"Google Colab\n", | |
"google@colab.fake\n", | |
"/content/gdrive/My Drive\n", | |
"DLA2020\n", | |
"Cloning into 'DLA2020'...\n", | |
"remote: Enumerating objects: 182, done.\u001b[K\n", | |
"remote: Counting objects: 100% (182/182), done.\u001b[K\n", | |
"remote: Compressing objects: 100% (137/137), done.\u001b[K\n", | |
"remote: Total 182 (delta 61), reused 143 (delta 36), pack-reused 0\u001b[K\n", | |
"Receiving objects: 100% (182/182), 10.81 MiB | 1.93 MiB/s, done.\n", | |
"Resolving deltas: 100% (61/61), done.\n", | |
"/content/gdrive/My Drive/DLA2020\n", | |
"Already up to date.\n", | |
"total 24K\n", | |
"drwx------ 5 root root 4.0K Nov 23 17:18 \u001b[0m\u001b[01;34mAppendix\u001b[0m/\n", | |
"drwx------ 8 root root 4.0K Nov 23 17:18 \u001b[01;34m.git\u001b[0m/\n", | |
"-rw------- 1 root root 4.8K Nov 23 17:18 .gitignore\n", | |
"drwx------ 3 root root 4.0K Nov 23 17:18 \u001b[01;34mHomework\u001b[0m/\n", | |
"drwx------ 6 root root 4.0K Nov 23 17:18 \u001b[01;34mLectures\u001b[0m/\n", | |
"-rw------- 1 root root 2.6K Nov 23 17:18 README.md\n", | |
"-rw------- 1 root root 0 Nov 23 17:18 test_webhook\n" | |
], | |
"name": "stdout" | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "UpHi64jSMcDk" | |
}, | |
"source": [ | |
"# From now on you can use the power of the git command ;)\n", | |
"# Just use an exclamation mark <!> followed by yout git command.\n", | |
"# `!git ...`\n", | |
"#\n", | |
"# e.g.\n", | |
"# `!git pull`\n", | |
"# `!git status`\n", | |
"# `!git reset --hard HEAD`\n", | |
"# `!git add .`\n", | |
"# `!git commit -m \"nice stuff\"`\n", | |
"# `!git push`" | |
], | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "l_fc6KQzNRFZ" | |
}, | |
"source": [ | |
"# To run your python project:\n", | |
"%matplotlib inline\n", | |
"%run -i main.py" | |
], | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "BpPXg7U1q2tm" | |
}, | |
"source": [ | |
"#@title TensorBoard\n", | |
"LOG_DIRECTORY = \"./log\" #@param {type:\"string\"}\n", | |
"\n", | |
"# load and display tensorboard\n", | |
"%reload_ext tensorboard\n", | |
"%tensorboard --logdir LOG_DIRECTORY" | |
], | |
"execution_count": null, | |
"outputs": [] | |
} | |
] | |
} |
Author
HeinrichAD
commented
Nov 23, 2020
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment