Spaces:
Sleeping
Sleeping
Petr Tsvetkov
commited on
Commit
·
5602c70
1
Parent(s):
1f691bd
Create update function for diff view using diff2htmlUI js library instead of iframe
Browse files
app.py
CHANGED
@@ -18,38 +18,48 @@ n_samples = len(dataset)
|
|
18 |
saver = gr.HuggingFaceDatasetSaver(HF_TOKEN, HF_DATASET, private=True)
|
19 |
|
20 |
|
21 |
-
def
|
22 |
-
repo_url = f"https://github.com/{repo}/
|
23 |
return repo_url
|
24 |
|
25 |
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
53 |
|
54 |
|
55 |
def update_commit_view(sample_ind):
|
@@ -57,14 +67,15 @@ def update_commit_view(sample_ind):
|
|
57 |
return None
|
58 |
|
59 |
record = dataset[sample_ind]
|
60 |
-
|
61 |
|
62 |
-
github_link_md = f"[See the commit on GitHub]({github_link})"
|
63 |
-
|
|
|
64 |
commit_msg = record['message']
|
65 |
repo_val = record['repo']
|
66 |
hash_val = record['hash']
|
67 |
-
return
|
68 |
|
69 |
|
70 |
def next_sample(current_sample_ind, shuffled_idx):
|
@@ -76,7 +87,16 @@ def next_sample(current_sample_ind, shuffled_idx):
|
|
76 |
return (current_sample_ind,) + updated_view
|
77 |
|
78 |
|
79 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
80 |
repo_val = gr.Textbox(interactive=False, label='repo', visible=False)
|
81 |
hash_val = gr.Textbox(interactive=False, label='hash', visible=False)
|
82 |
shuffled_idx_val = gr.JSON(visible=False)
|
@@ -94,7 +114,7 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
94 |
skip_btn = gr.Button("Skip the current sample")
|
95 |
with gr.Row():
|
96 |
with gr.Column(scale=2):
|
97 |
-
github_link = gr.Markdown()
|
98 |
diff_view = gr.HTML()
|
99 |
with gr.Column(scale=1):
|
100 |
commit_msg = gr.Textbox(label="Commit message",
|
@@ -126,7 +146,7 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
126 |
label='session')
|
127 |
|
128 |
commit_view = [
|
129 |
-
github_link,
|
130 |
diff_view,
|
131 |
commit_msg,
|
132 |
repo_val,
|
@@ -164,6 +184,6 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
164 |
return (session, shuffled_idx) + update_commit_view(shuffled_idx[current_sample])
|
165 |
|
166 |
|
167 |
-
|
168 |
|
169 |
-
|
|
|
18 |
saver = gr.HuggingFaceDatasetSaver(HF_TOKEN, HF_DATASET, private=True)
|
19 |
|
20 |
|
21 |
+
def get_github_api_url(repo, hash):
|
22 |
+
repo_url = f"https://api.github.com/repos/{repo}/commits/{hash}"
|
23 |
return repo_url
|
24 |
|
25 |
|
26 |
+
DIFF_VIEW_UPDATE_JS_FN = """
|
27 |
+
<script>
|
28 |
+
function updateDiffView() {
|
29 |
+
var github_api_url = document.getElementById('diff-view').getAttribute("github-api-url");
|
30 |
+
var xmlHttp = new XMLHttpRequest();
|
31 |
+
xmlHttp.onreadystatechange = function() {{
|
32 |
+
if (xmlHttp.readyState == 4 && xmlHttp.status == 200)
|
33 |
+
var diff = xmlHttp.responseText;
|
34 |
+
console.log(diff);
|
35 |
+
var targetElement = document.getElementById('diff-view');
|
36 |
+
var configuration = {
|
37 |
+
drawFileList: true,
|
38 |
+
matching: 'lines',
|
39 |
+
highlight: true
|
40 |
+
};
|
41 |
+
var diff2htmlUi = new Diff2HtmlUI(targetElement, diff, configuration);
|
42 |
+
diff2htmlUi.draw();
|
43 |
+
}}
|
44 |
+
xmlHttp.open("GET", github_api_url, true);
|
45 |
+
xmlHttp.setRequestHeader("Accept", "application/vnd.github.v3.diff");
|
46 |
+
xmlHttp.send();
|
47 |
+
}
|
48 |
+
</script>
|
49 |
+
"""
|
50 |
+
|
51 |
+
|
52 |
+
def get_diff2html_view(github_api_url):
|
53 |
+
html = f"""
|
54 |
+
<div style='width:100%; height:720px; overflow:auto'>
|
55 |
+
<div
|
56 |
+
id='diff-view'
|
57 |
+
github-api-url="{github_api_url}"
|
58 |
+
></div>
|
59 |
+
</div>
|
60 |
+
"""
|
61 |
+
|
62 |
+
return html
|
63 |
|
64 |
|
65 |
def update_commit_view(sample_ind):
|
|
|
67 |
return None
|
68 |
|
69 |
record = dataset[sample_ind]
|
70 |
+
github_api_url = get_github_api_url(record['repo'], record['hash'])
|
71 |
|
72 |
+
# github_link_md = f"[See the commit on GitHub]({github_link})"
|
73 |
+
|
74 |
+
diff_view = get_diff2html_view(github_api_url)
|
75 |
commit_msg = record['message']
|
76 |
repo_val = record['repo']
|
77 |
hash_val = record['hash']
|
78 |
+
return diff_view, commit_msg, repo_val, hash_val
|
79 |
|
80 |
|
81 |
def next_sample(current_sample_ind, shuffled_idx):
|
|
|
87 |
return (current_sample_ind,) + updated_view
|
88 |
|
89 |
|
90 |
+
DIFF2HTML_IMPORTS = """
|
91 |
+
<!-- Stylesheet -->
|
92 |
+
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.8.0/styles/github.min.css" />
|
93 |
+
<link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/npm/diff2html/bundles/css/diff2html.min.css" />
|
94 |
+
|
95 |
+
<!-- Javascripts -->
|
96 |
+
<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/diff2html/bundles/js/diff2html-ui.min.js"></script>
|
97 |
+
"""
|
98 |
+
|
99 |
+
with gr.Blocks(theme=gr.themes.Soft(), head=DIFF2HTML_IMPORTS + DIFF_VIEW_UPDATE_JS_FN) as application:
|
100 |
repo_val = gr.Textbox(interactive=False, label='repo', visible=False)
|
101 |
hash_val = gr.Textbox(interactive=False, label='hash', visible=False)
|
102 |
shuffled_idx_val = gr.JSON(visible=False)
|
|
|
114 |
skip_btn = gr.Button("Skip the current sample")
|
115 |
with gr.Row():
|
116 |
with gr.Column(scale=2):
|
117 |
+
# github_link = gr.Markdown()
|
118 |
diff_view = gr.HTML()
|
119 |
with gr.Column(scale=1):
|
120 |
commit_msg = gr.Textbox(label="Commit message",
|
|
|
146 |
label='session')
|
147 |
|
148 |
commit_view = [
|
149 |
+
# github_link,
|
150 |
diff_view,
|
151 |
commit_msg,
|
152 |
repo_val,
|
|
|
184 |
return (session, shuffled_idx) + update_commit_view(shuffled_idx[current_sample])
|
185 |
|
186 |
|
187 |
+
application.load(init_session, inputs=[current_sample_sld], outputs=[session_val, shuffled_idx_val] + commit_view)
|
188 |
|
189 |
+
application.launch()
|