Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
Validata
Validata UI
Commits
90ca360a
Commit
90ca360a
authored
Nov 18, 2021
by
Pierre Dittgen
Browse files
Improve home page loading speed using background update
parent
e1edd7b1
Changes
2
Hide whitespace changes
Inline
Side-by-side
validata_ui/ui_util.py
View file @
90ca360a
"""Util UI functions."""
import
threading
from
flask
import
flash
...
...
@@ -20,3 +22,34 @@ def flash_success(msg):
def
flash_info
(
msg
):
"""Flash bootstrap info message."""
flash
(
msg
,
"info"
)
class
ThreadUpdater
:
"""Fire last known value and update on background."""
def
__init__
(
self
,
func
):
# First time run to get an initial value
self
.
val
=
func
()
self
.
func
=
func
self
.
thread
=
None
@
property
def
value
(
self
):
self
.
_background_update
()
return
self
.
val
def
_background_update
(
self
):
# Don't run if an update is already running
if
self
.
thread
is
not
None
:
return
# Run func to update self.val and reset self.thread
# to show that background update is done.
def
doIt
():
self
.
val
=
self
.
func
()
self
.
thread
=
None
# Init and start thread
self
.
thread
=
threading
.
Thread
(
target
=
doIt
)
self
.
thread
.
start
()
validata_ui/views.py
View file @
90ca360a
...
...
@@ -27,7 +27,7 @@ from validata_core.helpers import (
from
.
import
app
,
config
,
fetch_schema
,
pdf_service
,
schema_catalog_registry
from
.model
import
Section
from
.ui_util
import
flash_error
,
flash_warning
from
.ui_util
import
ThreadUpdater
,
flash_error
,
flash_warning
from
.validata_util
import
strip_accents
log
=
logging
.
getLogger
(
__name__
)
...
...
@@ -556,67 +556,70 @@ def retrieve_schema_catalog(section: Section):
# Routes
@
app
.
route
(
"/"
)
def
home
():
"""Home page."""
def
iter_sections
():
"""Yield sections of the home page, filled with schema metadata."""
# Iterate on all sections
for
section
in
config
.
CONFIG
.
homepage
.
sections
:
def
iter_sections
():
"""Yield sections of the home page, filled with schema metadata."""
# Iterate on all
section
s
for
section
in
config
.
CONFIG
.
homepage
.
sections
:
# section with only links to external validators
if
section
.
links
:
yield
section
continue
# section with
only links to external validators
if
section
.
links
:
yield
section
continue
# section with
catalog
if
section
.
catalog
is
None
:
# skip
section
continue
# section with catalog
if
section
.
catalog
is
None
:
# skip section
continue
# retrieving schema catatalog
schema_catalog
,
catalog_error
=
retrieve_schema_catalog
(
section
)
if
schema_catalog
is
None
:
yield
catalog_error
continue
# retrieving schema catatalog
schema_catalog
,
catalog_error
=
retrieve_schema_catalog
(
section
)
if
schema_catalog
is
None
:
yield
catalog_error
# Working on catalog
schema_info_list
=
[]
for
schema_reference
in
schema_catalog
.
references
:
# retain tableschema only
if
schema_reference
.
get_schema_type
()
!=
"tableschema"
:
continue
# Working on catalog
schema_info_list
=
[]
for
schema_reference
in
schema_catalog
.
references
:
# retain tableschema only
if
schema_reference
.
get_schema_type
()
!=
"tableschema"
:
continue
# Loads default table schema for each schema reference
schema_info
=
{
"name"
:
schema_reference
.
name
}
try
:
table_schema
=
fetch_schema
(
schema_reference
.
get_schema_url
())
except
json
.
JSONDecodeError
:
schema_info
[
"err"
]
=
True
schema_info
[
"title"
]
=
(
f
"le format du schéma «
{
schema_info
[
'name'
]
}
» "
"n'est pas reconnu"
)
except
Exception
:
schema_info
[
"err"
]
=
True
schema_info
[
"title"
]
=
(
f
"le schéma «
{
schema_info
[
'name'
]
}
» "
"n'est pas disponible"
)
else
:
schema_info
[
"title"
]
=
(
table_schema
.
get
(
"title"
)
or
schema_info
[
"name"
]
)
schema_info_list
.
append
(
schema_info
)
schema_info_list
=
sorted
(
schema_info_list
,
key
=
lambda
sc
:
strip_accents
(
sc
[
"title"
].
lower
())
)
# Loads default table schema for each schema reference
schema_info
=
{
"name"
:
schema_reference
.
name
}
try
:
table_schema
=
fetch_schema
(
schema_reference
.
get_schema_url
())
except
json
.
JSONDecodeError
:
schema_info
[
"err"
]
=
True
schema_info
[
"title"
]
=
(
f
"le format du schéma «
{
schema_info
[
'name'
]
}
» "
"n'est pas reconnu"
)
except
Exception
:
schema_info
[
"err"
]
=
True
schema_info
[
"title"
]
=
(
f
"le schéma «
{
schema_info
[
'name'
]
}
» "
"n'est pas disponible"
)
else
:
schema_info
[
"title"
]
=
table_schema
.
get
(
"title"
)
or
schema_info
[
"name"
]
schema_info_list
.
append
(
schema_info
)
schema_info_list
=
sorted
(
schema_info_list
,
key
=
lambda
sc
:
strip_accents
(
sc
[
"title"
].
lower
())
)
yield
{
**
{
k
:
v
for
k
,
v
in
section
.
dict
().
items
()
if
k
!=
"catalog"
},
"catalog"
:
schema_info_list
,
}
yield
{
**
{
k
:
v
for
k
,
v
in
section
.
dict
().
items
()
if
k
!=
"catalog"
},
"catalog"
:
schema_info_list
,
}
section_updater
=
ThreadUpdater
(
lambda
:
list
(
iter_sections
()))
@
app
.
route
(
"/"
)
def
home
():
"""Home page."""
return
render_template
(
"home.html"
,
config
=
config
,
sections
=
list
(
iter_sections
())
)
return
render_template
(
"home.html"
,
config
=
config
,
sections
=
section_updater
.
value
)
@
app
.
route
(
"/pdf"
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment