Skip to content

Commit 8a79105

Browse files
committed
reveal notebook in frontend when create success
1 parent 059fb5a commit 8a79105

File tree

8 files changed

+53
-21
lines changed

8 files changed

+53
-21
lines changed

jupyterlab_leetcode/handlers/leetcode_handler.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -351,8 +351,6 @@ async def post(self):
351351
self.finish({"message": "Question not found"})
352352
return
353353

354-
self.log.info(question)
355-
356354
notebook_generator = self.settings.get("notebook_generator")
357355
if not notebook_generator:
358356
notebook_generator = NotebookGenerator()

jupyterlab_leetcode/utils/notebook_generator.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,9 +137,9 @@ def __populate_run(self, snippet):
137137
func_name = func_match[1]
138138
run_cell["source"] = [f"Solution().{func_name}()"]
139139

140-
def __dump(self, q, base_dir="."):
140+
def __dump(self, q):
141141
qid = q["questionFrontendId"]
142-
directory = os.path.join(base_dir, get_folder_for(int(qid), 50))
142+
directory = get_folder_for(int(qid), 50)
143143
if not os.path.exists(directory):
144144
os.mkdir(directory)
145145

src/components/LeetCode.tsx

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
import React, { useEffect, useState } from 'react';
2+
import { IDocumentManager } from '@jupyterlab/docmanager';
23
import { getProfile } from '../services/leetcode';
34
import { LeetCodeProfile } from '../types/leetcode';
45
import Profile from './Profile';
56
import Statistics from './Statistics';
67
import QuestionList from './QuestionList';
78

8-
const LeetCode: React.FC = () => {
9+
const LeetCode: React.FC<{ docManager: IDocumentManager }> = ({
10+
docManager
11+
}) => {
912
const [profile, setProfile] = useState<LeetCodeProfile | null>(null);
1013

1114
useEffect(() => {
@@ -18,11 +21,15 @@ const LeetCode: React.FC = () => {
1821
});
1922
}, []);
2023

24+
const openNoteBook = (path: string) => {
25+
docManager.openOrReveal(path);
26+
};
27+
2128
return profile ? (
2229
<div>
2330
<Profile profile={profile} />
2431
<Statistics username={profile.username} />
25-
<QuestionList />
32+
<QuestionList openNotebook={openNoteBook} />
2633
</div>
2734
) : null;
2835
};

src/components/QuestionItem.tsx

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@ import React from 'react';
22
import { LeetCodeQuestion } from '../types/leetcode';
33
import { generateNotebook } from '../services/notebook';
44

5-
const QuestionItem: React.FC<{ question: LeetCodeQuestion }> = ({
6-
question
7-
}) => {
5+
const QuestionItem: React.FC<{
6+
question: LeetCodeQuestion;
7+
onGenerateSuccess: (p: string) => void;
8+
}> = ({ question, onGenerateSuccess }) => {
89
return (
910
<div>
1011
<a
@@ -13,7 +14,19 @@ const QuestionItem: React.FC<{ question: LeetCodeQuestion }> = ({
1314
>
1415
{question.title}
1516
</a>
16-
<button onClick={() => generateNotebook(question.titleSlug)}>C</button>
17+
<button
18+
onClick={() => {
19+
generateNotebook(question.titleSlug).then(r => {
20+
console.log('generateNotebook', r);
21+
if (r) {
22+
const { filePath } = r;
23+
onGenerateSuccess(filePath);
24+
}
25+
});
26+
}}
27+
>
28+
C
29+
</button>
1730
</div>
1831
);
1932
};

src/components/QuestionList.tsx

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ import { listQuestions } from '../services/leetcode';
33
import { LeetCodeQuestion } from '../types/leetcode';
44
import QuestionItem from './QuestionItem';
55

6-
const QuestionList: React.FC = () => {
6+
const QuestionList: React.FC<{ openNotebook: (p: string) => void }> = ({
7+
openNotebook
8+
}) => {
79
const [skip, setSkip] = useState(0);
810
const limit = 100;
911
const [keyword, setKeyword] = useState('');
@@ -44,7 +46,11 @@ const QuestionList: React.FC = () => {
4446
{questions.length > 0 ? (
4547
<div>
4648
{questions.map(q => (
47-
<QuestionItem key={q.id} question={q} />
49+
<QuestionItem
50+
key={q.id}
51+
question={q}
52+
onGenerateSuccess={(path: string) => openNotebook(path)}
53+
/>
4854
))}
4955
</div>
5056
) : null}

src/index.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
ILayoutRestorer
55
} from '@jupyterlab/application';
66
import { ICommandPalette, WidgetTracker } from '@jupyterlab/apputils';
7+
import { IDocumentManager } from '@jupyterlab/docmanager';
78

89
import LeetCodeWidget from './widget';
910

@@ -16,11 +17,12 @@ const plugin: JupyterFrontEndPlugin<void> = {
1617
id: PLUGIN_ID,
1718
description: 'Integrate LeetCode into beloved Jupyter.',
1819
autoStart: true,
19-
requires: [ICommandPalette],
20+
requires: [ICommandPalette, IDocumentManager],
2021
optional: [ILayoutRestorer],
2122
activate: (
2223
app: JupyterFrontEnd,
2324
palette: ICommandPalette,
25+
docManager: IDocumentManager,
2426
restorer: ILayoutRestorer | null
2527
) => {
2628
let leetcodeWidget: LeetCodeWidget;
@@ -30,7 +32,7 @@ const plugin: JupyterFrontEndPlugin<void> = {
3032
label: 'Open LeetCode Widget',
3133
execute: () => {
3234
if (!leetcodeWidget || leetcodeWidget.isDisposed) {
33-
leetcodeWidget = new LeetCodeWidget();
35+
leetcodeWidget = new LeetCodeWidget(docManager);
3436
}
3537
if (!tracker.has(leetcodeWidget)) {
3638
tracker.add(leetcodeWidget);

src/services/notebook.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import { requestAPI } from './handler';
22

33
export async function generateNotebook(titleSlug: string) {
4-
return requestAPI<{ ok: boolean }>('/notebook/create', {
4+
return requestAPI<{ filePath: string }>('/notebook/create', {
55
method: 'POST',
66
body: JSON.stringify({ titleSlug })
7-
}).catch(() => ({ ok: false }));
7+
}).catch(() => null);
88
}

src/widget.tsx

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
import { ReactWidget } from '@jupyterlab/ui-components';
2+
import { IDocumentManager } from '@jupyterlab/docmanager';
23
import React, { StrictMode, useEffect, useState } from 'react';
34
import LandingPage from './components/LandingPage';
45
import LeetCode from './components/LeetCode';
56
import { getCookie } from './services/cookie';
67

7-
const LeetCodeComponent = (): JSX.Element => {
8+
const LeetCodeComponent: React.FC<{ docManager: IDocumentManager }> = ({
9+
docManager
10+
}) => {
811
const [cookieLoggedIn, setCookieLoggedIn] = useState('');
912

1013
useEffect(() => {
@@ -22,23 +25,26 @@ const LeetCodeComponent = (): JSX.Element => {
2225
});
2326

2427
return cookieLoggedIn ? (
25-
<LeetCode />
28+
<LeetCode docManager={docManager} />
2629
) : (
2730
<LandingPage setCookieLoggedIn={b => setCookieLoggedIn(b)} />
2831
);
2932
};
3033

3134
class LeetCodeWidget extends ReactWidget {
32-
constructor() {
35+
docManager: IDocumentManager;
36+
37+
constructor(docManager: IDocumentManager) {
3338
super();
3439
this.id = 'JupyterlabLeetcodeWidget';
3540
this.addClass('jupyterlab-leetcode-widget');
41+
this.docManager = docManager;
3642
}
3743

38-
protected render(): JSX.Element {
44+
render(): JSX.Element {
3945
return (
4046
<StrictMode>
41-
<LeetCodeComponent />
47+
<LeetCodeComponent docManager={this.docManager} />
4248
</StrictMode>
4349
);
4450
}

0 commit comments

Comments
 (0)