Skip to content

Commit 25e58ac

Browse files
Introduce Nebari support in code generator (#314)
* Introduce Nebari support in code generator * fix UI for nebari button and issues in netlify functions for nebari url * fix some formatting issues * Change the nebari.js functions * Update src/components/NavNebari.vue Co-authored-by: vfdev <vfdev.5@gmail.com> * message fix for util.js function * Fixes alignment issues in NavNebari and validUserName func for special characters, Add copyCommand func * Remove Copy Nebari link and make the links open in new tab * fix generating link error * fix UI for nebari details and open in Nebari button * changed the UI of Nebari link button * Change bottom margin of the Open In Nebari button * Refactoring colab.js and Nebari.js for common code in creating notebook cells --------- Co-authored-by: vfdev <vfdev.5@gmail.com>
1 parent ef156f6 commit 25e58ac

File tree

6 files changed

+501
-62
lines changed

6 files changed

+501
-62
lines changed

functions/colab.js

Lines changed: 4 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { pushToGitHub, getZip_Uid } from './utils'
1+
import { pushToGitHub, getZip_Uid, getNbCells } from './utils'
22

33
const repoOwner = process.env.VUE_APP_GH_USER
44
const repo = process.env.VUE_APP_GH_REPO
@@ -11,73 +11,16 @@ exports.handler = async function (event, _) {
1111
const template = `ignite-${data.template}`
1212
const nbName = `${template}.ipynb`
1313
const { zipRes, nbUid } = await getZip_Uid(data)
14-
14+
const argparser = data.argparser
1515
const title = template
1616
.replace('ignite-', '')
1717
.split('-')
1818
.map((v) => v[0].toUpperCase() + v.slice(1))
1919
.join(' ')
20-
// notebook cell structure
21-
22-
function create_nb_cell(source_array, cell_type) {
23-
return {
24-
cell_type: cell_type,
25-
metadata: {},
26-
execution_count: null,
27-
outputs: [],
28-
source: source_array
29-
}
30-
}
31-
32-
let specific_commands = []
33-
34-
if (title === 'Template Vision Segmentation') {
35-
specific_commands.push(
36-
'!python -c "from data import download_datasets; download_datasets(\'./\')"'
37-
)
38-
}
3920

40-
const md_cell = [
41-
`# ${title} by PyTorch-Ignite Code-Generator\n\n`,
42-
'Please, run the cell below to execute your code.'
43-
]
21+
// get notebook cell structure
22+
const nb = getNbCells(title, zipRes, argparser, template)
4423

45-
const common_nb_commands = [
46-
`!wget ${zipRes}\n`,
47-
`!unzip ${template}.zip\n`,
48-
'!pip install -r requirements.txt'
49-
]
50-
51-
const argparser = data.argparser
52-
const execution_nb_commands = [
53-
`!python main.py ${
54-
argparser === 'hydra'
55-
? '#--config-dir=/content/ --config-name=config.yaml'
56-
: 'config.yaml'
57-
}`
58-
]
59-
60-
let nb_cells = [
61-
create_nb_cell(md_cell, 'markdown'),
62-
create_nb_cell(common_nb_commands, 'code')
63-
]
64-
if (specific_commands.length > 0) {
65-
nb_cells.push(create_nb_cell(specific_commands, 'code'))
66-
}
67-
nb_cells.push(create_nb_cell(execution_nb_commands, 'code'))
68-
69-
const nb = {
70-
nbformat: 4,
71-
nbformat_minor: 0,
72-
metadata: {
73-
kernelspec: {
74-
display_name: 'Python 3',
75-
name: 'python3'
76-
},
77-
accelerator: 'GPU'
78-
},
79-
cells: nb_cells
80-
}
8124
// Create the notebook on GitHub
8225
await pushToGitHub(
8326
Buffer.from(JSON.stringify(nb)).toString('base64'),

functions/nebari.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import {
2+
pushToGitHub,
3+
getZip_Uid,
4+
getRootUrlWithoutTrailingSlash,
5+
getNbCells
6+
} from './utils'
7+
8+
const repoOwner = process.env.VUE_APP_GH_USER
9+
const repo = process.env.VUE_APP_GH_REPO
10+
11+
// This function is the one Netlify function runs on
12+
// https://docs.netlify.com/functions/build-with-javascript/#synchronous-function-format
13+
exports.handler = async function (event, _) {
14+
// event is a JSON object
15+
const data = JSON.parse(event.body)
16+
const template = `ignite-${data.template}`
17+
const nebariInstanceLink = getRootUrlWithoutTrailingSlash(
18+
data.nebariInstanceLink
19+
)
20+
const argparser = data.argparser
21+
const userName = data.userName
22+
const nbName = `${template}.ipynb`
23+
const { zipRes, nbUid } = await getZip_Uid(data)
24+
25+
const title = template
26+
.replace('ignite-', '')
27+
.split('-')
28+
.map((v) => v[0].toUpperCase() + v.slice(1))
29+
.join(' ')
30+
31+
// get notebook cell structure
32+
const nb = getNbCells(title, zipRes, argparser, template)
33+
34+
// Create the notebook on GitHub
35+
await pushToGitHub(
36+
Buffer.from(JSON.stringify(nb)).toString('base64'),
37+
nbName,
38+
nbUid
39+
)
40+
41+
const nebariLink = `${nebariInstanceLink}/user/${userName}/lab/tree/GitHub%3A${repoOwner}/${repo}/nbs/${nbUid}/${nbName}`
42+
43+
return {
44+
statusCode: 200,
45+
body: nebariLink
46+
}
47+
}

functions/utils.js

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,11 @@ export async function pushToGitHub(content, filename, nbUid) {
5757
}
5858
}
5959

60+
/**
61+
* Create a file on GitHub with Octokit.
62+
* @param {JSON} data
63+
* @returns zipRes, nbUid
64+
*/
6065
// This function is the one Netlify function runs on
6166
// https://docs.netlify.com/functions/build-with-javascript/#synchronous-function-format
6267
export async function getZip_Uid(data) {
@@ -84,3 +89,87 @@ export async function getZip_Uid(data) {
8489
nbUid: nbUid
8590
}
8691
}
92+
93+
/**
94+
* Extracts the root URL from a given URL, excluding trailing slashes.
95+
* @param {string} url
96+
* @returns {string} rootUrl
97+
*/
98+
export function getRootUrlWithoutTrailingSlash(url) {
99+
// Use the URL constructor to parse the input URL
100+
const parsedUrl = new URL(url)
101+
// Get the origin (root) part of the URL without a trailing slash
102+
const rootUrl = parsedUrl.origin.toString()
103+
104+
return rootUrl
105+
}
106+
107+
/**
108+
* Get the correct formatted notebook for Colab and Nebari functions
109+
* @param {string} title
110+
* @param {string} zipRes
111+
* @param {string} argparser
112+
* @param {string} template
113+
* @returns {JSON} nb
114+
*/
115+
export function getNbCells(title, zipRes, argparser, template) {
116+
function create_nb_cell(source_array, cell_type) {
117+
return {
118+
cell_type: cell_type,
119+
metadata: {},
120+
execution_count: null,
121+
outputs: [],
122+
source: source_array
123+
}
124+
}
125+
126+
let specific_commands = []
127+
128+
if (title === 'Template Vision Segmentation') {
129+
specific_commands.push(
130+
'!python -c "from data import download_datasets; download_datasets(\'./\')"'
131+
)
132+
}
133+
134+
const md_cell = [
135+
`# ${title} by PyTorch-Ignite Code-Generator\n\n`,
136+
'Please, run the cell below to execute your code.'
137+
]
138+
139+
const common_nb_commands = [
140+
`!wget ${zipRes}\n`,
141+
`!unzip ${template}.zip\n`,
142+
'!pip install -r requirements.txt'
143+
]
144+
145+
const execution_nb_commands = [
146+
`!python main.py ${
147+
argparser === 'hydra'
148+
? '#--config-dir=/content/ --config-name=config.yaml'
149+
: 'config.yaml'
150+
}`
151+
]
152+
153+
let nb_cells = [
154+
create_nb_cell(md_cell, 'markdown'),
155+
create_nb_cell(common_nb_commands, 'code')
156+
]
157+
if (specific_commands.length > 0) {
158+
nb_cells.push(create_nb_cell(specific_commands, 'code'))
159+
}
160+
nb_cells.push(create_nb_cell(execution_nb_commands, 'code'))
161+
162+
const nb = {
163+
nbformat: 4,
164+
nbformat_minor: 0,
165+
metadata: {
166+
kernelspec: {
167+
display_name: 'Python 3',
168+
name: 'python3'
169+
},
170+
accelerator: 'GPU'
171+
},
172+
cells: nb_cells
173+
}
174+
return nb
175+
}

src/assets/Nebari-logo-square.svg

Lines changed: 1 addition & 0 deletions
Loading

0 commit comments

Comments
 (0)