Skip to content

Commit 75f49d0

Browse files
author
Tim Shawver
committed
Python 2 support for unicode strings in columns, fix gaps in cell borders.
1 parent aa77764 commit 75f49d0

4 files changed

Lines changed: 58 additions & 17 deletions

File tree

js/src/qgrid.css

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -286,12 +286,12 @@
286286
.q-grid .slick-cell {
287287
border-bottom: 1px solid #e1e8ed;
288288
border-right: none;
289-
border-top: 1px solid transparent;
290289
border-left: 1px solid transparent;
291290
font-size: 13px;
292291
font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif;
293-
padding-top: 3px;
294292
padding-left: 0px;
293+
padding-top: 4px;
294+
border-top: none;
295295
}
296296

297297
.q-grid.highlight-selected-row .slick-cell.selected {
@@ -322,7 +322,16 @@
322322

323323
.q-grid .slick-cell.idx-col {
324324
font-weight: bold;
325+
margin-right: 3px;
326+
}
327+
328+
.q-grid .slick-cell.idx-col.last-idx-col {
325329
border-right: 1px solid rgb(225, 232, 237);
330+
}
331+
332+
.q-grid .slick-cell.idx-col:not(.first-idx-col) {
333+
font-weight: bold;
334+
border-left: 1px solid rgb(225, 232, 237);
326335
margin-right: 3px;
327336
}
328337

@@ -351,27 +360,27 @@
351360
.q-grid .slick-cell.l0.r0 {
352361
border-left: none;
353362
padding-left: 6px;
354-
z-index: 90;
363+
z-index: 85;
355364
}
356365

357366
.q-grid .slick-cell.l1.r1 {
358-
z-index: 89;
367+
z-index: 86;
359368
}
360369

361370
.q-grid .slick-cell.l2.r2 {
362-
z-index: 88;
371+
z-index: 87;
363372
}
364373

365374
.q-grid .slick-cell.l3.r3 {
366-
z-index: 87;
375+
z-index: 88;
367376
}
368377

369378
.q-grid .slick-cell.l4.r4 {
370-
z-index: 86;
379+
z-index: 89;
371380
}
372381

373382
.q-grid .slick-cell.l5.r5 {
374-
z-index: 85;
383+
z-index: 90;
375384
}
376385

377386
.q-grid .slick-cell.selected {

js/src/qgrid.widget.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,13 @@ class QgridView extends widgets.DOMWidgetView {
347347
if (cur_column.is_index) {
348348
slick_column.editor = editors.IndexEditor;
349349
slick_column.cssClass += ' idx-col';
350+
if (cur_column.first_index){
351+
slick_column.cssClass += ' first-idx-col';
352+
}
353+
if (cur_column.last_index){
354+
slick_column.cssClass += ' last-idx-col';
355+
}
356+
350357
slick_column.name = cur_column.index_display_text;
351358
slick_column.level = cur_column.level;
352359
this.index_columns.push(slick_column);

qgrid/grid.py

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
)
2020
from itertools import chain
2121
from uuid import uuid4
22+
from six import string_types
2223

2324
# versions of pandas prior to version 0.20.0 don't support the orient='table'
2425
# when calling the 'to_json' function on DataFrames. to get around this we
@@ -354,6 +355,13 @@ def show_grid(data_frame, show_toolbar=None,
354355
PAGE_SIZE = 100
355356

356357

358+
def stringify(x):
359+
if isinstance(x, string_types):
360+
return x
361+
else:
362+
return str(x)
363+
364+
357365
@widgets.register()
358366
class QgridWidget(widgets.DOMWidget):
359367
"""
@@ -794,7 +802,7 @@ def should_be_stringified(col_series):
794802
else:
795803
series_to_set = self._get_col_series_from_df(
796804
col_name, df, level_vals=True
797-
).map(str)
805+
).map(stringify)
798806
self._set_col_series_on_df(col_name, df, series_to_set)
799807

800808
if type(df.index) == pd.core.index.MultiIndex and \
@@ -809,7 +817,7 @@ def should_be_stringified(col_series):
809817
prev_idx = row_loc - 1
810818
for idx, index_val in enumerate(index):
811819
col_name = self._primary_key[idx]
812-
if previous_value == None:
820+
if previous_value is None:
813821
row_style[col_name] = 'group-top'
814822
continue
815823
elif index_val == previous_value[idx]:
@@ -823,11 +831,16 @@ def should_be_stringified(col_series):
823831
else:
824832
row_style[col_name] = 'group-middle'
825833
else:
826-
row_style[col_name] = 'single' if last_row else 'group-top'
834+
if last_row:
835+
row_style[col_name] = 'single'
836+
else:
837+
row_style[col_name] = 'group-top'
827838
if prev_idx >= 0:
828-
if row_styles[prev_idx][col_name] == 'group-middle':
839+
if row_styles[prev_idx][col_name] == \
840+
'group-middle':
829841
row_styles[prev_idx][col_name] = 'group-bottom'
830-
elif row_styles[prev_idx][col_name] == 'group-top':
842+
elif row_styles[prev_idx][col_name] == \
843+
'group-top':
831844
row_styles[prev_idx][col_name] = 'group-single'
832845
previous_value = index
833846
row_styles[row_loc] = row_style
@@ -855,9 +868,13 @@ def should_be_stringified(col_series):
855868
if ('primaryKey' in df_schema):
856869
self._primary_key = df_schema['primaryKey']
857870
else:
858-
# for some reason, 'primaryKey' isn't set when the index is
859-
# a single interval column. that's why this case is here.
860-
self._primary_key = [df.index.name]
871+
# for some reason, 'primaryKey' isn't set in certain cases,
872+
# like when we have an interval index. that's why this case
873+
# is here.
874+
if df.index.name is not None:
875+
self._primary_key = [df.index.name]
876+
else:
877+
self._primary_key = ['index']
861878

862879
columns = {}
863880
for i, cur_column in enumerate(df_schema['fields']):
@@ -876,6 +893,11 @@ def should_be_stringified(col_series):
876893
self._primary_key_display[col_name]
877894
if len(self._primary_key) > 0:
878895
cur_column['level'] = self._primary_key.index(col_name)
896+
level = self._primary_key.index(col_name)
897+
if level == 0:
898+
cur_column['first_index'] = True
899+
if level == (len(self._primary_key) - 1):
900+
cur_column['last_index'] = True
879901

880902
cur_column['position'] = i
881903
columns[col_name] = cur_column

qgrid/tests/test_grid.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -541,7 +541,10 @@ def test_object_dtype():
541541

542542

543543
def test_index_categorical():
544-
df = pd.DataFrame({'foo': np.random.randn(3), 'future_index': [22, 13, 87]})
544+
df = pd.DataFrame({
545+
'foo': np.random.randn(3),
546+
'future_index': [22, 13, 87]
547+
})
545548
df['future_index'] = df['future_index'].astype('category')
546549
df = df.set_index('future_index')
547550
widget = QgridWidget(df=df)

0 commit comments

Comments
 (0)