Petr Tsvetkov commited on
Commit
fddb192
·
1 Parent(s): 7fbd3e2

Create update function for diff view using diff2htmlUI js library instead of iframe

Browse files
Files changed (1) hide show
  1. app.py +57 -39
app.py CHANGED
@@ -19,38 +19,48 @@ n_samples = len(dataset)
19
  saver = gr.HuggingFaceDatasetSaver(HF_TOKEN, HF_DATASET, private=True)
20
 
21
 
22
- def get_github_link(repo, hash):
23
- repo_url = f"https://github.com/{repo}/commit/{hash}"
24
  return repo_url
25
 
26
 
27
- def get_diff2html_demo_iframe(github_link):
28
- diff2html_link = (f"https://diff2html.xyz/demo.html?matching=none"
29
- f"&matchWordsThreshold=0.25"
30
- f"&maxLineLengthHighlight=10000"
31
- f"&diffStyle=word"
32
- f"&colorScheme=light"
33
- f"&renderNothingWhenEmpty=0"
34
- f"&matchingMaxComparisons=2500"
35
- f"&maxLineSizeInBlockForComparison=200"
36
- f"&outputFormat=line-by-line"
37
- f"&drawFileList=1&synchronisedScroll=1"
38
- f"&highlight=1"
39
- f"&fileListToggle=1"
40
- f"&fileListStartVisible=0"
41
- f"&highlightLanguages=[object%20Map]"
42
- f"&smartSelection=1"
43
- f"&fileContentToggle=1"
44
- f"&stickyFileHeaders=1"
45
- f"&diff={github_link}")
46
-
47
- iframe_html = (f"<iframe "
48
- f"src=\"{diff2html_link}\" "
49
- f"title=\"diff2html Demo Diff Viewer\" "
50
- f"style='width:100%; height:720px; overflow:auto'>"
51
- f"</iframe>")
52
-
53
- return iframe_html
 
 
 
 
 
 
 
 
 
 
54
 
55
 
56
  def update_commit_view(sample_ind):
@@ -58,15 +68,16 @@ def update_commit_view(sample_ind):
58
  return None
59
 
60
  record = dataset[sample_ind]
61
- github_link = get_github_link(record['repo'], record['hash'])
62
 
63
- github_link_md = f"[See the commit on GitHub]({github_link})"
64
- diff_view = get_diff2html_demo_iframe(github_link)
 
65
  commit_msg = record['message']
66
  repo_val = record['repo']
67
  hash_val = record['hash']
68
  diff_loaded_timestamp = datetime.now().isoformat()
69
- return github_link_md, diff_view, commit_msg, repo_val, hash_val, diff_loaded_timestamp
70
 
71
 
72
  def next_sample(current_sample_ind, shuffled_idx):
@@ -78,7 +89,16 @@ def next_sample(current_sample_ind, shuffled_idx):
78
  return (current_sample_ind,) + updated_view
79
 
80
 
81
- with gr.Blocks(theme=gr.themes.Soft()) as demo:
 
 
 
 
 
 
 
 
 
82
  repo_val = gr.Textbox(interactive=False, label='repo', visible=False)
83
  hash_val = gr.Textbox(interactive=False, label='hash', visible=False)
84
  shuffled_idx_val = gr.JSON(visible=False)
@@ -96,7 +116,6 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
96
  skip_btn = gr.Button("Skip the current sample")
97
  with gr.Row():
98
  with gr.Column(scale=2):
99
- github_link = gr.Markdown()
100
  diff_view = gr.HTML()
101
  with gr.Column(scale=1):
102
  commit_msg = gr.Textbox(label="Commit message",
@@ -136,7 +155,6 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
136
  label='submitted_ts')
137
 
138
  commit_view = [
139
- github_link,
140
  diff_view,
141
  commit_msg,
142
  repo_val,
@@ -155,7 +173,7 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
155
  format_feedback
156
  ]
157
 
158
- saver.setup([current_sample_sld] + feedback_form + [], "feedback")
159
 
160
  skip_btn.click(next_sample, inputs=[current_sample_sld, shuffled_idx_val],
161
  outputs=[current_sample_sld] + commit_view)
@@ -177,6 +195,6 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
177
  return (session, shuffled_idx) + update_commit_view(shuffled_idx[current_sample])
178
 
179
 
180
- demo.load(init_session, inputs=[current_sample_sld], outputs=[session_val, shuffled_idx_val] + commit_view)
181
 
182
- demo.launch()
 
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
+
63
+ return html
64
 
65
 
66
  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']
79
  diff_loaded_timestamp = datetime.now().isoformat()
80
+ return diff_view, commit_msg, repo_val, hash_val, diff_loaded_timestamp
81
 
82
 
83
  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)
 
116
  skip_btn = gr.Button("Skip the current sample")
117
  with gr.Row():
118
  with gr.Column(scale=2):
 
119
  diff_view = gr.HTML()
120
  with gr.Column(scale=1):
121
  commit_msg = gr.Textbox(label="Commit message",
 
155
  label='submitted_ts')
156
 
157
  commit_view = [
 
158
  diff_view,
159
  commit_msg,
160
  repo_val,
 
173
  format_feedback
174
  ]
175
 
176
+ saver.setup([current_sample_sld] + feedback_form, "feedback")
177
 
178
  skip_btn.click(next_sample, inputs=[current_sample_sld, shuffled_idx_val],
179
  outputs=[current_sample_sld] + commit_view)
 
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()