Skip to content

fix: open in colab #162

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Jun 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
110 changes: 110 additions & 0 deletions functions/colab.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
// Below imports are defined in
// `external_node_modules` of [functions] in netlify.toml
// They are required for this function to run

import { v4 as uuidv4 } from 'uuid'
import { Octokit } from '@octokit/core'
import JSZip from 'jszip'

const nbUid = uuidv4()
const repoOwner = process.env.VUE_APP_GH_USER
const repo = process.env.VUE_APP_GH_REPO

/**
* Create a file on GitHub with Octokit.
* @param {string} content
* @param {string} filename
* @returns download_url
*/
async function pushToGitHub(content, filename) {
const octokit = new Octokit({
auth: process.env.VUE_APP_GH_TOKEN
})
try {
const res = await octokit.request(
'PUT /repos/{owner}/{repo}/contents/{path}',
{
owner: repoOwner,
repo: repo,
path: `nbs/${nbUid}/${filename}`,
message: `nb: add ${nbUid}`,
content: content
}
)
// the download url is raw url - https://raw.githubusercontent.com/...
return res.data.content.download_url
} catch (e) {
console.error(e)
}
}

// This function is the one Netlify function runs on
// https://docs.netlify.com/functions/build-with-javascript/#synchronous-function-format
export async function handler(event, _) {
// event is a JSON object
const data = JSON.parse(event.body)
const zip = new JSZip()
const code = data.code
const template = `ignite-${data.template}`
const nbName = `${template}.ipynb`

// As usual from Download component,
// we will zip the files and
// generate a base64 format for pushing to GitHub
// with Octokit.
for (const filename in code) {
zip.file(filename, code[filename])
}
const content = await zip.generateAsync({ type: 'base64' })
const zipRes = await pushToGitHub(content, `${template}.zip`)

const title = template
.replace('ignite-', '')
.split('-')
.map((v) => v[0].toUpperCase() + v.slice(1))
.join(' ')
// notebook cell structure
const nb = {
nbformat: 4,
nbformat_minor: 0,
metadata: {
kernelspec: {
display_name: 'Python 3',
name: 'python3'
},
accelerator: 'GPU'
},
cells: [
{
cell_type: 'markdown',
metadata: {},
execution_count: null,
outputs: [],
source: [
`# ${title} by PyTorch-Ignite Code-Generator\n\n`,
'Please, run the cell below to execute your code.'
]
},
{
cell_type: 'code',
metadata: {},
execution_count: null,
outputs: [],
source: [
`!wget ${zipRes}\n`,
`!unzip ${template}.zip\n`,
'!pip install -r requirements.txt\n',
'!python main.py\n'
]
}
]
}
// Create the notebook on GitHub
await pushToGitHub(Buffer.from(JSON.stringify(nb)).toString('base64'), nbName)

const colabLink = `https://colab.research.google.com/github/${repoOwner}/${repo}/blob/main/nbs/${nbUid}/${nbName}`
return {
statusCode: 200,
body: colabLink
}
}
6 changes: 6 additions & 0 deletions netlify.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
[build.environment]
NODE_VERSION = "14"
PYTHON_VERSION = "3.7"
AWS_LAMBDA_JS_RUNTIME = "nodejs14.x"

[build]
publish = "./dist"
command = "npx pnpm i --frozen-lockfile --store=node_modules/.pnpm-store && npx pnpm run build"
functions = "functions"

[functions]
node_bundler = "esbuild"
external_node_modules = ["uuid", "jszip", "@octokit/core"]
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
},
"devDependencies": {
"@iconify/iconify": "^2.0.1",
"@octokit/core": "^3.5.1",
"@types/ejs": "^3.0.6",
"@types/file-saver": "^2.0.2",
"@types/jest": "^26.0.23",
Expand Down
118 changes: 118 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading