Petr Tsvetkov commited on
Commit
85b8fdd
·
1 Parent(s): fddb192

* Move html code to a separate file

Browse files

* Get rid of iframe
* Style overrides for diff2html

Files changed (3) hide show
  1. app.py +24 -51
  2. head.html +43 -0
  3. style_overrides.css +18 -0
app.py CHANGED
@@ -19,44 +19,24 @@ n_samples = len(dataset)
19
  saver = gr.HuggingFaceDatasetSaver(HF_TOKEN, HF_DATASET, private=True)
20
 
21
 
22
- def get_github_api_url(repo, hash):
23
- repo_url = f"https://api.github.com/repos/{repo}/commits/{hash}"
24
- return repo_url
25
-
26
-
27
- DIFF_VIEW_UPDATE_JS_FN = """
28
- <script>
29
- function updateDiffView() {
30
- var github_api_url = document.getElementById('diff-view').getAttribute("github-api-url");
31
- var xmlHttp = new XMLHttpRequest();
32
- xmlHttp.onreadystatechange = function() {{
33
- if (xmlHttp.readyState == 4 && xmlHttp.status == 200)
34
- var diff = xmlHttp.responseText;
35
- console.log(diff);
36
- var targetElement = document.getElementById('diff-view');
37
- var configuration = {
38
- drawFileList: true,
39
- matching: 'lines',
40
- highlight: true
41
- };
42
- var diff2htmlUi = new Diff2HtmlUI(targetElement, diff, configuration);
43
- diff2htmlUi.draw();
44
- }}
45
- xmlHttp.open("GET", github_api_url, true);
46
- xmlHttp.setRequestHeader("Accept", "application/vnd.github.v3.diff");
47
- xmlHttp.send();
48
- }
49
- </script>
50
- """
51
-
52
-
53
- def get_diff2html_view(github_api_url):
54
  html = f"""
55
- <div style='width:100%; height:720px; overflow:auto'>
56
- <div
57
- id='diff-view'
58
- github-api-url="{github_api_url}"
59
- ></div>
60
  </div>
61
  """
62
 
@@ -68,11 +48,8 @@ def update_commit_view(sample_ind):
68
  return None
69
 
70
  record = dataset[sample_ind]
71
- github_api_url = get_github_api_url(record['repo'], record['hash'])
72
 
73
- # github_link_md = f"[See the commit on GitHub]({github_link})"
74
-
75
- diff_view = get_diff2html_view(github_api_url)
76
  commit_msg = record['message']
77
  repo_val = record['repo']
78
  hash_val = record['hash']
@@ -89,16 +66,10 @@ def next_sample(current_sample_ind, shuffled_idx):
89
  return (current_sample_ind,) + updated_view
90
 
91
 
92
- DIFF2HTML_IMPORTS = """
93
- <!-- Stylesheet -->
94
- <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.8.0/styles/github.min.css" />
95
- <link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/npm/diff2html/bundles/css/diff2html.min.css" />
96
-
97
- <!-- Javascripts -->
98
- <script type="text/javascript" src="https://cdn.jsdelivr.net/npm/diff2html/bundles/js/diff2html-ui.min.js"></script>
99
- """
100
 
101
- with gr.Blocks(theme=gr.themes.Soft(), head=DIFF2HTML_IMPORTS + DIFF_VIEW_UPDATE_JS_FN) as application:
102
  repo_val = gr.Textbox(interactive=False, label='repo', visible=False)
103
  hash_val = gr.Textbox(interactive=False, label='hash', visible=False)
104
  shuffled_idx_val = gr.JSON(visible=False)
@@ -195,6 +166,8 @@ with gr.Blocks(theme=gr.themes.Soft(), head=DIFF2HTML_IMPORTS + DIFF_VIEW_UPDATE
195
  return (session, shuffled_idx) + update_commit_view(shuffled_idx[current_sample])
196
 
197
 
198
- application.load(init_session, inputs=[current_sample_sld], outputs=[session_val, shuffled_idx_val] + commit_view)
 
 
199
 
200
  application.launch()
 
19
  saver = gr.HuggingFaceDatasetSaver(HF_TOKEN, HF_DATASET, private=True)
20
 
21
 
22
+ def convert_diff_to_unified(diff):
23
+ result = "\n".join(
24
+ [
25
+ f'--- {modified_file["old_path"]}\n'
26
+ f'+++ {modified_file["new_path"]}\n'
27
+ f'{modified_file["diff"]}'
28
+ for modified_file in diff
29
+ ]
30
+ )
31
+
32
+ return result
33
+
34
+
35
+ def get_diff2html_view(raw_diff):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
36
  html = f"""
37
+ <div style='width:100%; height:720px; overflow:auto; position: relative'>
38
+ <div id='diff-raw' hidden>{raw_diff}</div>
39
+ <div id='diff-view'></div>
 
 
40
  </div>
41
  """
42
 
 
48
  return None
49
 
50
  record = dataset[sample_ind]
 
51
 
52
+ diff_view = get_diff2html_view(convert_diff_to_unified(record['mods']))
 
 
53
  commit_msg = record['message']
54
  repo_val = record['repo']
55
  hash_val = record['hash']
 
66
  return (current_sample_ind,) + updated_view
67
 
68
 
69
+ with open("head.html") as head_file:
70
+ head_html = head_file.read()
 
 
 
 
 
 
71
 
72
+ with gr.Blocks(theme=gr.themes.Soft(), head=head_html, css="style_overrides.css") as application:
73
  repo_val = gr.Textbox(interactive=False, label='repo', visible=False)
74
  hash_val = gr.Textbox(interactive=False, label='hash', visible=False)
75
  shuffled_idx_val = gr.JSON(visible=False)
 
166
  return (session, shuffled_idx) + update_commit_view(shuffled_idx[current_sample])
167
 
168
 
169
+ application.load(init_session,
170
+ inputs=[current_sample_sld],
171
+ outputs=[session_val, shuffled_idx_val] + commit_view, )
172
 
173
  application.launch()
head.html ADDED
@@ -0,0 +1,43 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <!-- Stylesheet -->
2
+ <link
3
+ rel="stylesheet"
4
+ href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.8.0/styles/github.min.css"
5
+ media="screen and (prefers-color-scheme: light)"
6
+ />
7
+ <link
8
+ rel="stylesheet"
9
+ href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.8.0/styles/github-dark.min.css"
10
+ media="screen and (prefers-color-scheme: dark)"
11
+ />
12
+ <link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/npm/diff2html/bundles/css/diff2html.min.css"/>
13
+
14
+ <!-- Javascripts -->
15
+ <script type="text/javascript" src="https://cdn.jsdelivr.net/npm/diff2html/bundles/js/diff2html-ui.min.js"></script>
16
+
17
+ <script>
18
+ var oldDiff = "";
19
+
20
+ function updateDiffView() {
21
+ try {
22
+ var diff = document.getElementById('diff-raw').innerHTML;
23
+ if (!diff || diff === oldDiff) {
24
+ return;
25
+ }
26
+ oldDiff = diff;
27
+ var targetElement = document.getElementById('diff-view');
28
+ var configuration = {
29
+ drawFileList: true,
30
+ matching: 'lines',
31
+ highlight: true
32
+ };
33
+ var diff2htmlUi = new Diff2HtmlUI(targetElement, diff, configuration);
34
+ diff2htmlUi.draw();
35
+ } catch (error) {
36
+
37
+ }
38
+ }
39
+
40
+ setInterval(function () {
41
+ updateDiffView();
42
+ }, 500);
43
+ </script>
style_overrides.css ADDED
@@ -0,0 +1,18 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ .d2h-diff-table {
2
+ .d2h-diff-tbody {
3
+ tr {
4
+ td {
5
+ padding: 0;
6
+ margin: 0;
7
+ }
8
+ }
9
+ }
10
+ }
11
+
12
+ .d2h-file-header {
13
+ .d2h-file-collapse {
14
+ .d2h-file-collapse-input {
15
+ margin: 0 4px 0 0;
16
+ }
17
+ }
18
+ }