Skip to content

Commit 8b73290

Browse files
committed
Try creating Flask app and use difflib
1 parent 0fb34d9 commit 8b73290

5 files changed

Lines changed: 106 additions & 0 deletions

File tree

src/app/README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# assignment-snapshots app
2+
3+
## Setup
4+
5+
1. Install Python 3.11.5
6+
2. Create a virtual environment: `python3 -m venv app-env`
7+
3. Activate the virtual environment: `source app-env/bin/activate`
8+
4. Install dependencies: `pip install -r requirements.txt`

src/app/main.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
from flask import Flask, request, render_template
2+
import difflib
3+
4+
app = Flask(__name__)
5+
6+
7+
@app.route("/", methods=["GET", "POST"])
8+
def index():
9+
if request.method == "POST":
10+
file1_content = request.form["file1"].splitlines()
11+
file2_content = request.form["file2"].splitlines()
12+
13+
differ = difflib.HtmlDiff()
14+
diff_html = differ.make_file(file1_content, file2_content)
15+
with open("templates/diff.html", "w") as f:
16+
f.write(diff_html)
17+
return render_template("diff.html", diff_html=diff_html)
18+
return render_template("index.html")
19+
20+
21+
if __name__ == "__main__":
22+
app.run(debug=True)

src/app/requirements.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
blinker==1.9.0
2+
click==8.1.8
3+
Flask==3.1.0
4+
itsdangerous==2.2.0
5+
Jinja2==3.1.6
6+
MarkupSafe==3.0.2
7+
Werkzeug==3.1.3

src/app/templates/diff.html

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
2+
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
3+
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
4+
5+
<html>
6+
7+
<head>
8+
<meta http-equiv="Content-Type"
9+
content="text/html; charset=utf-8" />
10+
<title></title>
11+
<style type="text/css">
12+
table.diff {font-family:Courier; border:medium;}
13+
.diff_header {background-color:#e0e0e0}
14+
td.diff_header {text-align:right}
15+
.diff_next {background-color:#c0c0c0}
16+
.diff_add {background-color:#aaffaa}
17+
.diff_chg {background-color:#ffff77}
18+
.diff_sub {background-color:#ffaaaa}
19+
</style>
20+
</head>
21+
22+
<body>
23+
24+
<table class="diff" id="difflib_chg_to4__top"
25+
cellspacing="0" cellpadding="0" rules="groups" >
26+
<colgroup></colgroup> <colgroup></colgroup> <colgroup></colgroup>
27+
<colgroup></colgroup> <colgroup></colgroup> <colgroup></colgroup>
28+
29+
<tbody>
30+
<tr><td class="diff_next" id="difflib_chg_to4__1"><a href="#difflib_chg_to4__1">n</a></td><td class="diff_header" id="from4_1">1</td><td nowrap="nowrap">Lorem&nbsp;ipsum&nbsp;dolor&nbsp;sit&nbsp;amet,&nbsp;consectetur&nbsp;adipiscing&nbsp;elit,&nbsp;<span class="diff_chg">sed</span>&nbsp;do&nbsp;eiusmod&nbsp;tempor&nbsp;incididunt&nbsp;ut&nbsp;labore&nbsp;et&nbsp;dolore&nbsp;magna&nbsp;aliqua.<span class="diff_sub">&nbsp;</span></td><td class="diff_next"><a href="#difflib_chg_to4__1">n</a></td><td class="diff_header" id="to4_1">1</td><td nowrap="nowrap">Lorem&nbsp;ipsum&nbsp;dolor&nbsp;sit&nbsp;amet,&nbsp;consectetur&nbsp;adipiscing&nbsp;elit,&nbsp;<span class="diff_chg">blah</span>&nbsp;do&nbsp;eiusmod&nbsp;tempor&nbsp;incididunt&nbsp;ut&nbsp;labore&nbsp;et&nbsp;dolore&nbsp;magna&nbsp;aliqua.</td></tr>
31+
<tr><td class="diff_next"></td><td class="diff_header" id="from4_2">2</td><td nowrap="nowrap">Ut&nbsp;enim&nbsp;ad&nbsp;minim&nbsp;veniam,&nbsp;quis&nbsp;nostrud&nbsp;exercitation&nbsp;ullamco&nbsp;laboris&nbsp;nisi&nbsp;ut&nbsp;aliquip&nbsp;ex&nbsp;ea&nbsp;commodo&nbsp;<span class="diff_chg">consequ</span>a<span class="diff_chg">t</span>.&nbsp;</td><td class="diff_next"></td><td class="diff_header" id="to4_2">2</td><td nowrap="nowrap">Ut&nbsp;enim&nbsp;ad&nbsp;minim&nbsp;veniam,&nbsp;quis&nbsp;nostrud&nbsp;exercitation&nbsp;ullamco&nbsp;laboris&nbsp;nisi&nbsp;ut&nbsp;aliquip&nbsp;ex&nbsp;ea&nbsp;commodo&nbsp;<span class="diff_chg">bl</span>a<span class="diff_chg">h</span>.&nbsp;</td></tr>
32+
<tr><td class="diff_next"></td><td class="diff_header" id="from4_3">3</td><td nowrap="nowrap">Duis&nbsp;aute&nbsp;irure&nbsp;dolor&nbsp;in&nbsp;reprehenderit&nbsp;in&nbsp;voluptate&nbsp;velit&nbsp;esse&nbsp;cillum&nbsp;dolore&nbsp;eu&nbsp;fugiat&nbsp;nulla&nbsp;pariatur.&nbsp;</td><td class="diff_next"></td><td class="diff_header" id="to4_3">3</td><td nowrap="nowrap">Duis&nbsp;aute&nbsp;irure&nbsp;dolor&nbsp;in&nbsp;reprehenderit&nbsp;in&nbsp;voluptate&nbsp;velit&nbsp;esse&nbsp;cillum&nbsp;dolore&nbsp;eu&nbsp;fugiat&nbsp;nulla&nbsp;pariatur.&nbsp;</td></tr>
33+
<tr><td class="diff_next"><a href="#difflib_chg_to4__top">t</a></td><td class="diff_header" id="from4_4">4</td><td nowrap="nowrap">Excepteur&nbsp;sint&nbsp;occaecat&nbsp;cupidatat&nbsp;non&nbsp;proident,&nbsp;<span class="diff_chg">sunt</span>&nbsp;in&nbsp;culpa&nbsp;qui&nbsp;officia&nbsp;deserunt&nbsp;mollit&nbsp;anim&nbsp;id&nbsp;est&nbsp;laborum</td><td class="diff_next"><a href="#difflib_chg_to4__top">t</a></td><td class="diff_header" id="to4_4">4</td><td nowrap="nowrap">Excepteur&nbsp;sint&nbsp;occaecat&nbsp;cupidatat&nbsp;non&nbsp;proident,&nbsp;<span class="diff_chg">blah</span>&nbsp;in&nbsp;culpa&nbsp;qui&nbsp;officia&nbsp;deserunt&nbsp;mollit&nbsp;anim&nbsp;id&nbsp;est&nbsp;laborum</td></tr>
34+
</tbody>
35+
</table>
36+
<table class="diff" summary="Legends">
37+
<tr> <th colspan="2"> Legends </th> </tr>
38+
<tr> <td> <table border="" summary="Colors">
39+
<tr><th> Colors </th> </tr>
40+
<tr><td class="diff_add">&nbsp;Added&nbsp;</td></tr>
41+
<tr><td class="diff_chg">Changed</td> </tr>
42+
<tr><td class="diff_sub">Deleted</td> </tr>
43+
</table></td>
44+
<td> <table border="" summary="Links">
45+
<tr><th colspan="2"> Links </th> </tr>
46+
<tr><td>(f)irst change</td> </tr>
47+
<tr><td>(n)ext change</td> </tr>
48+
<tr><td>(t)op</td> </tr>
49+
</table></td> </tr>
50+
</table>
51+
</body>
52+
53+
</html>

src/app/templates/index.html

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<title>File Diff</title>
5+
</head>
6+
<body>
7+
<h1>File Diff</h1>
8+
<form method="POST">
9+
<label for="file1">File 1:</label><br>
10+
<textarea id="file1" name="file1" rows="10" cols="50"></textarea><br><br>
11+
<label for="file2">File 2:</label><br>
12+
<textarea id="file2" name="file2" rows="10" cols="50"></textarea><br><br>
13+
<button type="submit">Compare</button>
14+
</form>
15+
</body>
16+
</html>

0 commit comments

Comments
 (0)