Skip to content

Commit

Permalink
Add helper method for determining environment
Browse files Browse the repository at this point in the history
  • Loading branch information
pgleeson committed Sep 14, 2023
1 parent 0e20626 commit 8892374
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 29 deletions.
33 changes: 4 additions & 29 deletions notebooks/neuroml.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,14 @@
"\n",
"## 1) Initial setup and library installs\n",
"\n",
"Note: the cell below runs some code to determine where the notebook is running (e.g. on your local machine or on Binder), and decides where to save results. If it is hidden, click on the dots (•••) to view."
"Note: the cell below runs some code to determine where the notebook is running (e.g. on your local machine or on Binder)."
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"editable": true,
"jupyter": {
"source_hidden": true
},
"slideshow": {
"slide_type": ""
},
Expand All @@ -53,37 +50,15 @@
"name": "stdout",
"output_type": "stream",
"text": [
"Results will be saved in /Users/padraig/git/combine-notebooks/notebooks/results\n"
"Current directory is: /Users/padraig/git/combine-notebooks/notebooks/results\n"
]
}
],
"source": [
"#note: metadata hides this cell on binder but it doesn't get hidden on colab\n",
"from pathlib import Path\n",
"\n",
"#determine if we're running on colab\n",
"try:\n",
" import google.colab\n",
" exec_env = \"colab\" #we seem to be on colab\n",
" print(\"Assuming this notebook is running on Google Colab\")\n",
"except:\n",
" exec_env = \"binder\" #assume it's binder\n",
"\n",
"if exec_env == \"colab\":\n",
" working_dir = f\"{Path.cwd()}/combine-notebooks/notebooks/results\"\n",
" !git clone https://github.com/combine-org/combine-notebooks\n",
" %cd combine-notebooks\n",
" !pip install .\n",
" %mkdir -p {working_dir}\n",
"\n",
"else:\n",
" #binder starts off in the notebook's folder\n",
" working_dir = f\"{Path.cwd()}/results\"\n",
"\n",
"import os\n",
"os.chdir(working_dir)\n",
"\n",
"print(\"Results will be saved in %s\"%working_dir)"
"from combine_notebooks.utils import determine_environment\n",
"working_dir = determine_environment()"
]
},
{
Expand Down
27 changes: 27 additions & 0 deletions src/combine_notebooks/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
from pathlib import Path
import os

def determine_environment():

#determine if we're running on Google Colab
try:
import google.colab
exec_env = "colab" #we seem to be on colab
print("Assuming this notebook is running on Google Colab")
except:
exec_env = "binder" #assume it's binder

if exec_env == "colab":
working_dir = f"{Path.cwd()}/combine-notebooks/notebooks/results"
os.system('git clone https://github.com/combine-org/combine-notebooks')
os.chdir('combine-notebooks')
os.system('pip install .')

else:
#binder starts off in the notebook's folder
working_dir = f"{Path.cwd()}/results"

os.chdir(working_dir)

print("Current directory is: %s"%working_dir)
return working_dir

0 comments on commit 8892374

Please sign in to comment.