Skip to content
This repository has been archived by the owner on Sep 3, 2022. It is now read-only.

Commit

Permalink
Implement --live option for faster development. (#1321)
Browse files Browse the repository at this point in the history
  • Loading branch information
jimmc authored Apr 19, 2017
1 parent 67fbc17 commit ed8bacf
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 21 deletions.
8 changes: 8 additions & 0 deletions containers/datalab/content/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -114,5 +114,13 @@ then
FOREVER_CMD="${FOREVER_CMD} -s"
fi

if [ -d /devroot ]; then
# For development purposes, if the user has mapped a /devroot dir, use it.
echo "Running notebook server in live mode"
export DATALAB_LIVE_STATIC_DIR=/devroot/sources/web/datalab/static
export DATALAB_LIVE_TEMPLATES_DIR=/devroot/sources/web/datalab/templates
# Make sure we have the latest compiled typescript output
cp -p /devroot/build/web/nb/*.js /datalab/web
fi
echo "Open your browser to http://localhost:8081/ to connect to Datalab."
${FOREVER_CMD} /datalab/web/app.js
49 changes: 35 additions & 14 deletions containers/datalab/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -20,27 +20,48 @@
# command prompt, which is useful for tinkering within the container before
# manually starting the server.

HERE=$(dirname $0)
CONTENT=$HOME
ENTRYPOINT="/datalab/run.sh"
if [ "$1" != "" ]; then
if [ "$1" != "shell" ]; then
CONTENT=$1
shift
fi
if [ "$1" == "shell" ]; then
ENTRYPOINT="/bin/bash"
fi
fi
DEVROOT_DOCKER_OPTION=''
LIVE_MODE=1

# Use this flag to map in web server content during development
# -v $REPO_DIR/sources/web:/sources \
#
# To turn on debug logs, add the following:
# -e 'DATALAB_SETTINGS_OVERRIDES={"consoleLogLevel": "debug" }' \
function setup_live_mode() {
# Live mode makes the datalab container use a live copy of the
# development directory so your changes are visible to the container
# without rebuilding.
echo "Setting up live mode"
DEVROOT=$(cd "$HERE" && realpath ../..)
DEVROOT_DOCKER_OPTION="-v $DEVROOT:/devroot"
}

while [ $# -gt 0 ]; do
case "$1" in
shell)
ENTRYPOINT="/bin/bash"
;;
--no-live)
LIVE_MODE=0
;;
-*) echo "Unrecognized option '$1'"
exit 1
;;
*)
# For any other non-option argument, assume it is the content root.
CONTENT="$1"
;;
esac
shift
done

if [[ $LIVE_MODE == 1 ]]; then
setup_live_mode
fi

docker run -it --entrypoint=$ENTRYPOINT \
-p 127.0.0.1:8081:8080 \
-v "$CONTENT:/content" \
${DEVROOT_DOCKER_OPTION} \
-e "PROJECT_ID=$PROJECT_ID" \
-e "DATALAB_ENV=local" \
-e "DATALAB_DEBUG=true" \
Expand Down
15 changes: 9 additions & 6 deletions sources/web/datalab/jupyter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,8 @@ var callbackManager: callbacks.CallbackManager = new callbacks.CallbackManager()
/**
* Templates
*/
var templates: common.Map<string> = {
const templates: common.Map<string> = {
// These cached templates can be overridden in sendTemplate
'tree': fs.readFileSync(path.join(__dirname, 'templates', 'tree.html'), { encoding: 'utf8' }),
'sessions': fs.readFileSync(path.join(__dirname, 'templates', 'sessions.html'), { encoding: 'utf8' }),
'edit': fs.readFileSync(path.join(__dirname, 'templates', 'edit.html'), { encoding: 'utf8' }),
Expand Down Expand Up @@ -393,16 +394,18 @@ function getBaseTemplateData(request: http.ServerRequest): common.Map<string> {
}

function sendTemplate(key: string, data: common.Map<string>, response: http.ServerResponse) {
var template = templates[key];
let template = templates[key];

// NOTE: Uncomment to use external templates mapped into the container.
// This is only useful when actively developing the templates themselves.
// template = fs.readFileSync('/sources/datalab/templates/' + key + '.html', { encoding: 'utf8' });
// Set this env var to point to source directory for live updates without restart.
const liveTemplatesDir = process.env.DATALAB_LIVE_TEMPLATES_DIR
if (liveTemplatesDir) {
template = fs.readFileSync(path.join(liveTemplatesDir, key + '.html'), { encoding: 'utf8' });
}

// Replace <%name%> placeholders with actual values.
// TODO: Error handling if template placeholders are out-of-sync with
// keys in passed in data object.
var htmlContent = template.replace(/\<\%(\w+)\%\>/g, function(match, name) {
const htmlContent = template.replace(/\<\%(\w+)\%\>/g, function(match, name) {
return data[name];
});

Expand Down
10 changes: 9 additions & 1 deletion sources/web/datalab/static.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,15 @@ function sendFile(filePath: string, response: http.ServerResponse,
* @param response the out-going response associated with the current HTTP request.
*/
function sendDataLabFile(filePath: string, response: http.ServerResponse) {
sendFile(path.join(__dirname, 'static', filePath), response);
let live = false
let staticDir = path.join(__dirname, 'static')
// Set this env var to point to source directory for live updates without restart.
const liveStaticDir = process.env.DATALAB_LIVE_STATIC_DIR
if (liveStaticDir) {
live = true
staticDir = liveStaticDir
}
sendFile(path.join(staticDir, filePath), response, '', live);
}

/**
Expand Down

0 comments on commit ed8bacf

Please sign in to comment.