Skip to content

Commit f5ce8c9

Browse files
author
donaldh
authored
Merge pull request #101 from cgiraldo/containers
Support directories and non .ipynb files in jupyter component application
2 parents 202be7c + 034f1a8 commit f5ce8c9

File tree

3 files changed

+64
-18
lines changed

3 files changed

+64
-18
lines changed

api/src/main/resources/plugins/jupyter.py

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
import json
2626
import os
2727
import logging
28+
import shutil
29+
import stat
2830

2931
import deployer_utils
3032
from plugins.base_creator import Creator
@@ -67,25 +69,34 @@ def create_component(self, staged_component_path, application_name, user_name, c
6769
## Create local git repo for application_user if not exist.
6870
repo_path = '{}/jupyter-{}'.format(self._config['git_repos_root'], application_user)
6971
if os.path.isdir(repo_path) == False:
70-
os.system('mkdir -p {}'.format(repo_path))
72+
os.makedirs(repo_path)
7173
os.system('git init {}'.format(repo_path))
72-
os.system('cp {}/.git/hooks/post-update.sample {}/.git/hooks/post-update'.format(repo_path,repo_path))
73-
os.system('chmod a+x {}/.git/hooks/post-update'.format(repo_path))
74-
os.system('chmod a+w {} -R'.format(repo_path))
74+
shutil.copyfile(repo_path+'/.git/hooks/post-update.sample', repo_path+'/.git/hooks/post-update')
75+
os.chmod(repo_path+'/.git/hooks/post-update',0o755)
7576
this_dir = os.path.dirname(os.path.realpath(__file__))
76-
os.system('cp {}/jupyter_README.md {}/README.md'.format(this_dir, repo_path))
77-
os.system('cd {0} && git add README.md && git commit -m "Initial commit"'.format(repo_path))
77+
shutil.copyfile(this_dir+'/jupyter_README.ipynb',repo_path+'/README.ipynb')
78+
os.system('cd {0} && git add README.ipynb && git commit -m "Initial commit"'.format(repo_path))
7879
## add notebooks to application_user github repo.
79-
notebook_install_path = '{}/{}/'.format(repo_path, application_name)
80-
os.system('mkdir -p {}'.format(notebook_install_path))
80+
notebook_install_path = '{}/{}'.format(repo_path, application_name)
81+
os.makedirs('{}'.format(notebook_install_path))
8182
file_list = component['component_detail']
8283
for file_name in file_list:
83-
if file_name.endswith(r'.ipynb'):
84-
self._fill_properties('%s/%s' % (staged_component_path, file_name), properties)
85-
86-
logging.debug('Copying {} to {}'.format(file_name, notebook_install_path))
87-
os.system('cp {}/{} {}'.format( staged_component_path, file_name, notebook_install_path ))
88-
# update local github repo:
84+
# We copy all files in package to jupyter folder to let the user work with all kind of files/datasets.
85+
#if file_name.endswith(r'.ipynb'):
86+
if file_name != 'properties.json':
87+
if os.path.isfile('{}/{}'.format(staged_component_path,file_name)):
88+
self._fill_properties('%s/%s' % (staged_component_path, file_name), properties)
89+
logging.debug('Copying {} to {}'.format(file_name, notebook_install_path))
90+
shutil.copyfile('{}/{}'.format(staged_component_path, file_name),
91+
'{}/{}'.format(notebook_install_path, file_name ))
92+
else:
93+
logging.debug('creating {}/{} folder'.format(notebook_install_path, file_name))
94+
os.makedirs('{}/{}'.format(notebook_install_path, file_name))
95+
# Create a properties.json file in notebooks to access application jupyter component properties.
96+
with open('{}/properties.json'.format(notebook_install_path), 'w') as prop_file:
97+
prop_dict = { k.replace('component_',''): v for k, v in properties.items() if k.startswith('component_')}
98+
json.dump(prop_dict, prop_file)
99+
# update local github repo:
89100

90101
os.system('cd {0} && git add {1} && git commit -m "added {1} app notebooks"'.format(repo_path, application_name))
91102
delete_commands.append('rm -rf {}\n'.format( notebook_install_path))
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"# Notebooks of PNDA Applications\n",
8+
"\n",
9+
"This folder synchronizes the notebooks of the deployed PNDA Applications when your jupyter server starts. Each PNDA app is checkout in a folder with its name.\n",
10+
"\n",
11+
"> **WARNING**: WATCH OUT! modfications to notebooks or files in this folders will be removed when your jupyter server restarts.\n",
12+
"> If you want to persist your changes, copy the files outside this folder.\n",
13+
"\n",
14+
"\n"
15+
]
16+
}
17+
],
18+
"metadata": {
19+
"kernelspec": {
20+
"display_name": "Python 3",
21+
"language": "python",
22+
"name": "python3"
23+
},
24+
"language_info": {
25+
"codemirror_mode": {
26+
"name": "ipython",
27+
"version": 3
28+
},
29+
"file_extension": ".py",
30+
"mimetype": "text/x-python",
31+
"name": "python",
32+
"nbconvert_exporter": "python",
33+
"pygments_lexer": "ipython3",
34+
"version": "3.7.3"
35+
}
36+
},
37+
"nbformat": 4,
38+
"nbformat_minor": 4
39+
}

api/src/main/resources/plugins/jupyter_README.md

Lines changed: 0 additions & 4 deletions
This file was deleted.

0 commit comments

Comments
 (0)