Skip to content

Commit 1165584

Browse files
authored
Merge pull request #16 from manics/offline-lazy-db
Lazy load IndexedDB
2 parents 6f8822c + c53b44a commit 1165584

File tree

3 files changed

+33
-21
lines changed

3 files changed

+33
-21
lines changed

README.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ Jupyter Offline Notebook
33

44
[![Binder](https://mybinder.org/badge_logo.svg)](https://mybinder.org/v2/gh/manics/jupyter-offlinenotebook/master?filepath=example.ipynb)
55

6-
Save and load notebooks to local-storage, even if you've lost your connection to the server.
6+
Save and load notebooks to browser storage, even if you've lost your connection to the server.
77

88

99
Installation
@@ -66,3 +66,12 @@ There are [several major limitations](https://github.com/manics/jupyter-offlinen
6666
- Local-storage is limited by quotas imposed by the browser.
6767
- A repository ID and path of the notebook within Jupyter Notebook are used, joined by a ` `.
6868
This may change in future.
69+
70+
71+
Development notes
72+
-----------------
73+
74+
This extension stores notebooks in browser storage using the [IndexedDB API](https://developer.mozilla.org/en-US/docs/Web/API/IndexedDB_API), wrapped with [Dexie.js](https://dexie.org/).
75+
76+
One server API call is made during initialisation to obtain the storage configuration.
77+
Everything else is done client-side so should work even if the server is disconnected.

jupyter_offlinenotebook/static/main.js

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,29 +11,32 @@ define([
1111
var bindeRefUrl = null;
1212
var binderPersistentUrl = null
1313
var db = null;
14+
var dbname = 'jupyter-offlinenotebook';
1415

1516
var initialise = function() {
1617
$.getJSON(utils.get_body_data('baseUrl') + 'offlinenotebook/config', function(data) {
1718
repoid = data['repoid'];
1819
if (repoid) {
19-
console.log('local-storage repoid: ' + repoid);
20-
db = setupDb('jupyter-offlinenotebook');
20+
console.log('offline-notebook repoid: ' + repoid);
2121
}
2222
else {
23-
console.log('local-storage repoid not found, disabled');
23+
console.log('offline-notebook repoid not found, disabled');
2424
}
2525
bindeRefUrl = data['binder_ref_url'];
26-
console.log('local-storage bindeRefUrl: ' + bindeRefUrl);
26+
console.log('offline-notebook bindeRefUrl: ' + bindeRefUrl);
2727
binderPersistentUrl = data['binder_persistent_url']
28-
console.log('local-storage binderPersistentUrl: ' + binderPersistentUrl);
28+
console.log('offline-notebook binderPersistentUrl: ' + binderPersistentUrl);
2929
addButtons();
3030
});
3131
}
3232

33-
var setupDb = function(dbname) {
34-
var db = new dexie(dbname);
35-
// Only define indexed fields. pk: primary key
36-
db.version(1).stores({'offlinenotebook': 'pk,repoid,name,type'})
33+
var getDb = function() {
34+
if (!db) {
35+
db = new dexie(dbname);
36+
// Only define indexed fields. pk: primary key
37+
db.version(1).stores({'offlinenotebook': 'pk,repoid,name,type'});
38+
console.log('offline-notebook: Opened IndexedDB');
39+
}
3740
return db;
3841
}
3942

@@ -44,12 +47,12 @@ define([
4447
'handler': downloadNotebookFromBrowser
4548
}, 'offline-notebook-download', 'offlinenotebook');
4649
var saveAction = Jupyter.actions.register({
47-
'help': 'Save to local-storage',
50+
'help': 'Save to browser storage',
4851
'icon' : 'fa-download',
4952
'handler': localstoreSaveNotebook
5053
}, 'offline-notebook-save', 'offlinenotebook');
5154
var loadAction = Jupyter.actions.register({
52-
'help': 'Load from local-storage',
55+
'help': 'Load from browser storage',
5356
'icon' : 'fa-upload',
5457
'handler': localstoreLoadNotebook
5558
}, 'offline-notebook-load', 'offlinenotebook');
@@ -119,7 +122,7 @@ define([
119122
var path = Jupyter.notebook.notebook_path;
120123
var primaryKey = 'repoid:' + repoid + ' path:' + path;
121124
var nb = getNotebookFromBrowser();
122-
db.offlinenotebook.put({
125+
getDb().offlinenotebook.put({
123126
'pk': primaryKey,
124127
'repoid': repoid,
125128
'name': Jupyter.notebook.notebook_name,
@@ -128,8 +131,8 @@ define([
128131
'type': 'notebook',
129132
'content': nb
130133
}).then(function(key) {
131-
console.log('local-storage saved: ', key);
132-
modalDialog('Notebook saved to local-storage', key);
134+
console.log('offline-notebook saved: ', key);
135+
modalDialog('Notebook saved to browser storage', key);
133136
}).catch(function(e) {
134137
var body = $('<div/>').append(
135138
$('<div/>', {
@@ -146,15 +149,15 @@ define([
146149
function localstoreLoadNotebook() {
147150
var path = Jupyter.notebook.notebook_path;
148151
var primaryKey = 'repoid:' + repoid + ' path:' + path;
149-
db.offlinenotebook.get(primaryKey).then(function(nb) {
152+
getDb().offlinenotebook.get(primaryKey).then(function(nb) {
150153
if (nb) {
151154
Jupyter.notebook.fromJSON(nb);
152-
console.log('local-storage loaded ' + primaryKey);
153-
modalDialog('Loaded notebook from local-storage', primaryKey);
155+
console.log('offline-notebook loaded ' + primaryKey);
156+
modalDialog('Loaded notebook from browser storage', primaryKey);
154157
}
155158
else {
156-
console.log('local-storage not found ' + primaryKey);
157-
modalDialog('Notebook not found in local-storage', primaryKey, 'alert alert-danger');
159+
console.log('offline-notebook not found ' + primaryKey);
160+
modalDialog('Notebook not found in browser storage', primaryKey, 'alert alert-danger');
158161
}
159162
}).catch(function(e) {
160163
var body = $('<div/>').append(

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
setup(
55
name='jupyter-offlinenotebook',
6-
version='0.0.7',
6+
version='0.0.8',
77
author='Simon Li',
88
author_email='[email protected]',
99
packages=[

0 commit comments

Comments
 (0)