Skip to content

Commit 1564bb9

Browse files
authored
fix: open in colab (#162)
1 parent 0677621 commit 1564bb9

File tree

6 files changed

+314
-188
lines changed

6 files changed

+314
-188
lines changed

functions/colab.js

+110
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
// Below imports are defined in
2+
// `external_node_modules` of [functions] in netlify.toml
3+
// They are required for this function to run
4+
5+
import { v4 as uuidv4 } from 'uuid'
6+
import { Octokit } from '@octokit/core'
7+
import JSZip from 'jszip'
8+
9+
const nbUid = uuidv4()
10+
const repoOwner = process.env.VUE_APP_GH_USER
11+
const repo = process.env.VUE_APP_GH_REPO
12+
13+
/**
14+
* Create a file on GitHub with Octokit.
15+
* @param {string} content
16+
* @param {string} filename
17+
* @returns download_url
18+
*/
19+
async function pushToGitHub(content, filename) {
20+
const octokit = new Octokit({
21+
auth: process.env.VUE_APP_GH_TOKEN
22+
})
23+
try {
24+
const res = await octokit.request(
25+
'PUT /repos/{owner}/{repo}/contents/{path}',
26+
{
27+
owner: repoOwner,
28+
repo: repo,
29+
path: `nbs/${nbUid}/${filename}`,
30+
message: `nb: add ${nbUid}`,
31+
content: content
32+
}
33+
)
34+
// the download url is raw url - https://raw.githubusercontent.com/...
35+
return res.data.content.download_url
36+
} catch (e) {
37+
console.error(e)
38+
}
39+
}
40+
41+
// This function is the one Netlify function runs on
42+
// https://docs.netlify.com/functions/build-with-javascript/#synchronous-function-format
43+
export async function handler(event, _) {
44+
// event is a JSON object
45+
const data = JSON.parse(event.body)
46+
const zip = new JSZip()
47+
const code = data.code
48+
const template = `ignite-${data.template}`
49+
const nbName = `${template}.ipynb`
50+
51+
// As usual from Download component,
52+
// we will zip the files and
53+
// generate a base64 format for pushing to GitHub
54+
// with Octokit.
55+
for (const filename in code) {
56+
zip.file(filename, code[filename])
57+
}
58+
const content = await zip.generateAsync({ type: 'base64' })
59+
const zipRes = await pushToGitHub(content, `${template}.zip`)
60+
61+
const title = template
62+
.replace('ignite-', '')
63+
.split('-')
64+
.map((v) => v[0].toUpperCase() + v.slice(1))
65+
.join(' ')
66+
// notebook cell structure
67+
const nb = {
68+
nbformat: 4,
69+
nbformat_minor: 0,
70+
metadata: {
71+
kernelspec: {
72+
display_name: 'Python 3',
73+
name: 'python3'
74+
},
75+
accelerator: 'GPU'
76+
},
77+
cells: [
78+
{
79+
cell_type: 'markdown',
80+
metadata: {},
81+
execution_count: null,
82+
outputs: [],
83+
source: [
84+
`# ${title} by PyTorch-Ignite Code-Generator\n\n`,
85+
'Please, run the cell below to execute your code.'
86+
]
87+
},
88+
{
89+
cell_type: 'code',
90+
metadata: {},
91+
execution_count: null,
92+
outputs: [],
93+
source: [
94+
`!wget ${zipRes}\n`,
95+
`!unzip ${template}.zip\n`,
96+
'!pip install -r requirements.txt\n',
97+
'!python main.py\n'
98+
]
99+
}
100+
]
101+
}
102+
// Create the notebook on GitHub
103+
await pushToGitHub(Buffer.from(JSON.stringify(nb)).toString('base64'), nbName)
104+
105+
const colabLink = `https://colab.research.google.com/github/${repoOwner}/${repo}/blob/main/nbs/${nbUid}/${nbName}`
106+
return {
107+
statusCode: 200,
108+
body: colabLink
109+
}
110+
}

netlify.toml

+6
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
[build.environment]
22
NODE_VERSION = "14"
33
PYTHON_VERSION = "3.7"
4+
AWS_LAMBDA_JS_RUNTIME = "nodejs14.x"
45

56
[build]
67
publish = "./dist"
78
command = "npx pnpm i --frozen-lockfile --store=node_modules/.pnpm-store && npx pnpm run build"
9+
functions = "functions"
10+
11+
[functions]
12+
node_bundler = "esbuild"
13+
external_node_modules = ["uuid", "jszip", "@octokit/core"]

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
},
1515
"devDependencies": {
1616
"@iconify/iconify": "^2.0.1",
17+
"@octokit/core": "^3.5.1",
1718
"@types/ejs": "^3.0.6",
1819
"@types/file-saver": "^2.0.2",
1920
"@types/jest": "^26.0.23",

pnpm-lock.yaml

+118
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)