Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
C
caosdb-server
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container registry
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
caosdb
Software
caosdb-server
Commits
2718f769
Commit
2718f769
authored
Oct 16, 2020
by
Henrik tom Wörden
Browse files
Options
Downloads
Patches
Plain Diff
up
parent
5d7ace23
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
scripting/bin/pandas_table_preview.py
+24
-12
24 additions, 12 deletions
scripting/bin/pandas_table_preview.py
scripting/bin/test_pandas_table_preview.py
+4
-2
4 additions, 2 deletions
scripting/bin/test_pandas_table_preview.py
with
28 additions
and
14 deletions
scripting/bin/pandas_table_preview.py
+
24
−
12
View file @
2718f769
...
@@ -23,6 +23,11 @@
...
@@ -23,6 +23,11 @@
# ** end header
# ** end header
#
#
"""
This script tries to read typical table data files (.csv etc.) with pandas and
creates a html (partial) representation of the table.
"""
import
logging
import
logging
import
os
import
os
import
sys
import
sys
...
@@ -72,17 +77,19 @@ def ending_is_valid(fipath):
...
@@ -72,17 +77,19 @@ def ending_is_valid(fipath):
return
get_ending
(
fipath
)
is
not
None
return
get_ending
(
fipath
)
is
not
None
def
read_file
(
fipath
):
def
read_file
(
fipath
,
ftype
):
"""
tries to read the provided file
"""
"""
tries to read the provided file
"""
ending
=
get_ending
(
fipath
)
try
:
try
:
if
ending
in
[
"
.xls
"
,
"
.xlsx
"
]:
if
ftype
in
[
"
.xls
"
,
"
.xlsx
"
]:
df
=
pd
.
read_excel
(
fipath
)
df
=
pd
.
read_excel
(
fipath
)
elif
ending
==
"
.tsv
"
:
elif
ftype
==
"
.tsv
"
:
df
=
pd
.
read_csv
(
fipath
,
sep
=
"
\t
"
)
df
=
pd
.
read_csv
(
fipath
,
sep
=
"
\t
"
)
elif
ending
==
"
.csv
"
:
elif
ftype
==
"
.csv
"
:
df
=
pd
.
read_csv
(
fipath
)
df
=
pd
.
read_csv
(
fipath
)
else
:
print
(
"
File type unknown: {}
"
.
format
(
ftype
))
raise
RuntimeError
(
""
)
except
Exception
:
except
Exception
:
raise
ValueError
()
raise
ValueError
()
...
@@ -90,28 +97,33 @@ def read_file(fipath):
...
@@ -90,28 +97,33 @@ def read_file(fipath):
def
create_table_preview
(
fi
):
def
create_table_preview
(
fi
):
if
not
ending_is_valid
:
if
not
ending_is_valid
(
fi
.
path
)
:
print
(
"
Cannot create preview for Entity with ID={}, because download
"
print
(
"
Cannot create preview for Entity with ID={}, because download
"
"
failed.
"
.
format
(
entity_id
))
"
failed.
"
.
format
(
entity_id
))
sys
.
exit
(
1
)
return
ending
=
get_ending
(
fi
.
path
)
if
not
size_is_ok
(
fi
):
if
not
size_is_ok
(
fi
):
print
(
"
Skipped creating a preview for Entity with ID={}, because the
"
print
(
"
Skipped creating a preview for Entity with ID={}, because the
"
"
file is large!
"
.
format
(
entity_id
))
"
file is large!
"
.
format
(
entity_id
))
sys
.
exit
(
2
)
return
try
:
try
:
fipath
=
fi
.
download
()
tmpfile
=
fi
.
download
()
except
Exception
:
except
Exception
:
print
(
"
Cannot create preview for Entity with ID={}, because download
"
print
(
"
Cannot create preview for Entity with ID={}, because download
"
"
failed.
"
.
format
(
entity_id
))
"
failed.
"
.
format
(
entity_id
))
sys
.
exit
(
3
)
return
try
:
try
:
df
=
read_file
(
fipath
)
df
=
read_file
(
tmpfile
,
ending
)
except
ValueError
:
except
ValueError
:
print
(
"
Cannot read File Entity with ID={}.
"
.
format
(
entity_id
))
print
(
"
Cannot read File Entity with ID={}.
"
.
format
(
entity_id
))
sys
.
exit
(
4
)
return
print
(
df
.
to_html
(
max_cols
=
10
,
max_rows
=
10
))
print
(
df
.
to_html
(
max_cols
=
10
,
max_rows
=
10
))
...
...
This diff is collapsed.
Click to expand it.
scripting/bin/test_pandas_table_preview.py
+
4
−
2
View file @
2718f769
...
@@ -60,9 +60,11 @@ class PreviewTest(unittest.TestCase):
...
@@ -60,9 +60,11 @@ class PreviewTest(unittest.TestCase):
files
=
[
"
test.csv
"
,
"
test.tsv
"
,
"
test.xls
"
,
"
test.xlsx
"
]
files
=
[
"
test.csv
"
,
"
test.tsv
"
,
"
test.xls
"
,
"
test.xlsx
"
]
for
fi
in
files
:
for
fi
in
files
:
assert
fi
.
split
(
"
.
"
)[
1
]
+
"
file
"
in
read_file
(
fi
)
assert
fi
.
split
(
"
.
"
)[
1
]
+
"
file
"
in
read_file
(
fi
,
ftype
=
"
.
"
+
fi
.
split
(
"
.
"
)[
1
])
badfiles
=
[
"
bad.csv
"
,
"
bad.tsv
"
,
"
bad.xls
"
,
"
bad.xlsx
"
]
badfiles
=
[
"
bad.csv
"
,
"
bad.tsv
"
,
"
bad.xls
"
,
"
bad.xlsx
"
]
for
bfi
in
badfiles
:
for
bfi
in
badfiles
:
self
.
assertRaises
(
ValueError
,
read_file
,
bfi
)
self
.
assertRaises
(
ValueError
,
read_file
,
bfi
,
"
.
"
+
bfi
.
split
(
"
.
"
)[
1
])
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