Skip to content

Commit 8892374

Browse files
committed
Add helper method for determining environment
1 parent 0e20626 commit 8892374

File tree

2 files changed

+31
-29
lines changed

2 files changed

+31
-29
lines changed

notebooks/neuroml.ipynb

Lines changed: 4 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -30,17 +30,14 @@
3030
"\n",
3131
"## 1) Initial setup and library installs\n",
3232
"\n",
33-
"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."
33+
"Note: the cell below runs some code to determine where the notebook is running (e.g. on your local machine or on Binder)."
3434
]
3535
},
3636
{
3737
"cell_type": "code",
3838
"execution_count": 1,
3939
"metadata": {
4040
"editable": true,
41-
"jupyter": {
42-
"source_hidden": true
43-
},
4441
"slideshow": {
4542
"slide_type": ""
4643
},
@@ -53,37 +50,15 @@
5350
"name": "stdout",
5451
"output_type": "stream",
5552
"text": [
56-
"Results will be saved in /Users/padraig/git/combine-notebooks/notebooks/results\n"
53+
"Current directory is: /Users/padraig/git/combine-notebooks/notebooks/results\n"
5754
]
5855
}
5956
],
6057
"source": [
61-
"#note: metadata hides this cell on binder but it doesn't get hidden on colab\n",
6258
"from pathlib import Path\n",
6359
"\n",
64-
"#determine if we're running on colab\n",
65-
"try:\n",
66-
" import google.colab\n",
67-
" exec_env = \"colab\" #we seem to be on colab\n",
68-
" print(\"Assuming this notebook is running on Google Colab\")\n",
69-
"except:\n",
70-
" exec_env = \"binder\" #assume it's binder\n",
71-
"\n",
72-
"if exec_env == \"colab\":\n",
73-
" working_dir = f\"{Path.cwd()}/combine-notebooks/notebooks/results\"\n",
74-
" !git clone https://github.com/combine-org/combine-notebooks\n",
75-
" %cd combine-notebooks\n",
76-
" !pip install .\n",
77-
" %mkdir -p {working_dir}\n",
78-
"\n",
79-
"else:\n",
80-
" #binder starts off in the notebook's folder\n",
81-
" working_dir = f\"{Path.cwd()}/results\"\n",
82-
"\n",
83-
"import os\n",
84-
"os.chdir(working_dir)\n",
85-
"\n",
86-
"print(\"Results will be saved in %s\"%working_dir)"
60+
"from combine_notebooks.utils import determine_environment\n",
61+
"working_dir = determine_environment()"
8762
]
8863
},
8964
{

src/combine_notebooks/utils.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
from pathlib import Path
2+
import os
3+
4+
def determine_environment():
5+
6+
#determine if we're running on Google Colab
7+
try:
8+
import google.colab
9+
exec_env = "colab" #we seem to be on colab
10+
print("Assuming this notebook is running on Google Colab")
11+
except:
12+
exec_env = "binder" #assume it's binder
13+
14+
if exec_env == "colab":
15+
working_dir = f"{Path.cwd()}/combine-notebooks/notebooks/results"
16+
os.system('git clone https://github.com/combine-org/combine-notebooks')
17+
os.chdir('combine-notebooks')
18+
os.system('pip install .')
19+
20+
else:
21+
#binder starts off in the notebook's folder
22+
working_dir = f"{Path.cwd()}/results"
23+
24+
os.chdir(working_dir)
25+
26+
print("Current directory is: %s"%working_dir)
27+
return working_dir

0 commit comments

Comments
 (0)