Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
S
slide-index
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Welter, Felix
slide-index
Commits
27fcf5ea
Commit
27fcf5ea
authored
Sep 4, 2020
by
felixwelter
Browse files
Options
Downloads
Patches
Plain Diff
Add multi file upload and index purging
parent
356bbd5b
No related branches found
No related tags found
No related merge requests found
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
app.py
+31
-17
31 additions, 17 deletions
app.py
templates/index.html
+10
-5
10 additions, 5 deletions
templates/index.html
with
41 additions
and
22 deletions
app.py
+
31
−
17
View file @
27fcf5ea
import
glob
import
os
import
os
from
pathlib
import
Path
from
pathlib
import
Path
...
@@ -11,6 +12,7 @@ app = Flask(__name__)
...
@@ -11,6 +12,7 @@ app = Flask(__name__)
SLIDE_DIR
=
"
slides
"
SLIDE_DIR
=
"
slides
"
IMAGE_DIR
=
"
img_cache
"
IMAGE_DIR
=
"
img_cache
"
INDEX_DIR
=
"
index
"
Index
=
TitleFocusSearchIndex
Index
=
TitleFocusSearchIndex
...
@@ -18,6 +20,7 @@ Index = TitleFocusSearchIndex
...
@@ -18,6 +20,7 @@ Index = TitleFocusSearchIndex
@app.route
(
'
/
'
)
@app.route
(
'
/
'
)
def
index
():
def
index
():
ll
=
os
.
listdir
(
SLIDE_DIR
)
ll
=
os
.
listdir
(
SLIDE_DIR
)
ll
.
sort
()
return
render_template
(
'
index.html
'
,
ll
=
ll
)
return
render_template
(
'
index.html
'
,
ll
=
ll
)
...
@@ -27,15 +30,17 @@ def allowed_file(filename):
...
@@ -27,15 +30,17 @@ def allowed_file(filename):
@app.route
(
'
/upload
'
,
methods
=
[
'
POST
'
])
@app.route
(
'
/upload
'
,
methods
=
[
'
POST
'
])
def
upload
():
def
upload
():
if
'
file
'
in
request
.
files
:
if
'
files
'
in
request
.
files
:
file
=
request
.
files
[
'
file
'
]
files
=
request
.
files
.
getlist
(
'
files
'
)
for
i
,
file
in
enumerate
(
files
):
print
(
i
,
file
)
if
file
.
filename
!=
''
:
if
file
.
filename
!=
''
:
if
file
and
allowed_file
(
file
.
filename
):
if
file
and
allowed_file
(
file
.
filename
):
filename
=
secure_filename
(
file
.
filename
)
filename
=
secure_filename
(
file
.
filename
)
file_path
=
os
.
path
.
join
(
Path
(
SLIDE_DIR
),
filename
)
file_path
=
os
.
path
.
join
(
Path
(
SLIDE_DIR
),
filename
)
file
.
save
(
file_path
)
file
.
save
(
file_path
)
pdf
=
pdfplumber
.
open
(
file_path
)
pdf
=
pdfplumber
.
open
(
file_path
)
index
=
Index
()
index
=
Index
(
index_dir
=
INDEX_DIR
)
for
i
,
page
in
enumerate
(
pdf
.
pages
):
for
i
,
page
in
enumerate
(
pdf
.
pages
):
text
=
page
.
extract_text
()
text
=
page
.
extract_text
()
index
.
add
(
str
(
file_path
),
i
,
text
,
text
.
split
(
"
\n
"
)[
0
])
# Assumes title in the first line
index
.
add
(
str
(
file_path
),
i
,
text
,
text
.
split
(
"
\n
"
)[
0
])
# Assumes title in the first line
...
@@ -49,7 +54,7 @@ def upload():
...
@@ -49,7 +54,7 @@ def upload():
@app.route
(
"
/search
"
,
methods
=
[
'
POST
'
])
@app.route
(
"
/search
"
,
methods
=
[
'
POST
'
])
def
query
():
def
query
():
try
:
try
:
index
=
Index
()
index
=
Index
(
index_dir
=
INDEX_DIR
)
query
=
request
.
form
.
get
(
"
term
"
)
query
=
request
.
form
.
get
(
"
term
"
)
context
=
request
.
form
.
get
(
"
context
"
)
context
=
request
.
form
.
get
(
"
context
"
)
result
=
index
.
search
(
query
,
context
)
result
=
index
.
search
(
query
,
context
)
...
@@ -64,6 +69,15 @@ def query():
...
@@ -64,6 +69,15 @@ def query():
})
})
@app.route
(
"
/reset_index
"
,
methods
=
[
'
POST
'
])
def
reset_index
():
for
folder
in
[
IMAGE_DIR
,
SLIDE_DIR
,
INDEX_DIR
]:
files
=
glob
.
glob
(
folder
+
'
/*
'
)
for
f
in
files
:
os
.
remove
(
f
)
return
redirect
(
'
/
'
)
@app.route
(
"
/slide/<img_name>
"
)
@app.route
(
"
/slide/<img_name>
"
)
def
slide
(
img_name
):
def
slide
(
img_name
):
path
=
os
.
path
.
join
(
IMAGE_DIR
,
img_name
)
path
=
os
.
path
.
join
(
IMAGE_DIR
,
img_name
)
...
...
This diff is collapsed.
Click to expand it.
templates/index.html
+
10
−
5
View file @
27fcf5ea
...
@@ -5,13 +5,18 @@
...
@@ -5,13 +5,18 @@
<body>
<body>
<h1>
Upload new slide
</h1>
<h1>
Upload new slide
</h1>
<form
action=
"upload"
enctype=
"multipart/form-data"
method=
"post"
>
<form
action=
"upload"
enctype=
"multipart/form-data"
method=
"post"
>
<input
type=
"file"
name=
"file"
>
<input
type=
"file"
name=
"file
s"
multiple=
"
"
>
<input
type=
"submit"
value=
"Upload"
>
<input
type=
"submit"
value=
"Upload"
>
</form>
</form>
{% for item in ll %}
<ul>
<ul>
{% for item in ll %}
<li>
{{ item }}
</li>
<li>
{{ item }}
</li>
</ul>
{% endfor %}
{% endfor %}
</ul>
<form
action=
"reset_index"
method=
"post"
>
<input
type=
"submit"
value=
"Reset Index"
>
</form>
<h1>
Query
</h1>
<h1>
Query
</h1>
<form
action=
"search"
method=
"post"
>
<form
action=
"search"
method=
"post"
>
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment