Browse Source

fix: put_table()'s str cell format error

wangweimin 4 years ago
parent
commit
1112449229
3 changed files with 11 additions and 8 deletions
  1. 2 2
      pywebio/output.py
  2. 4 1
      test/11.charts.py
  3. 5 5
      webiojs/src/models/output.ts

+ 2 - 2
pywebio/output.py

@@ -400,11 +400,11 @@ def put_table(tdata, header=None, scope=Scope.Current, position=OutputPosition.B
     for x in range(len(tdata)):
     for x in range(len(tdata)):
         for y in range(len(tdata[x])):
         for y in range(len(tdata[x])):
             cell = tdata[x][y]
             cell = tdata[x][y]
-            if isinstance(cell, str):
-                tdata[x][y] = put_text(cell)
             if isinstance(cell, span_):
             if isinstance(cell, span_):
                 tdata[x][y] = cell.content
                 tdata[x][y] = cell.content
                 span['%s,%s' % (x, y)] = dict(col=cell.col, row=cell.row)
                 span['%s,%s' % (x, y)] = dict(col=cell.col, row=cell.row)
+            elif not isinstance(cell, Output):
+                tdata[x][y] = str(cell)
 
 
     spec = _get_output_spec('table', data=tdata, span=span, scope=scope, position=position)
     spec = _get_output_spec('table', data=tdata, span=span, scope=scope, position=position)
     return Output(spec)
     return Output(spec)

+ 4 - 1
test/11.charts.py

@@ -336,7 +336,10 @@ def plotly():
 
 
     html4 = fig.to_html(include_plotlyjs="require", full_html=False)
     html4 = fig.to_html(include_plotlyjs="require", full_html=False)
 
 
-    put_grid([[html1, html2], [html3, html4]], cell_width='1fr', cell_height='1fr')
+    put_grid([
+        [put_html(html1), put_html(html2)],
+        [put_html(html3), put_html(html4)]
+    ], cell_width='1fr', cell_height='1fr')
 
 
 
 
 def target():
 def target():

+ 5 - 5
webiojs/src/models/output.ts

@@ -95,7 +95,7 @@ let File = {
 
 
 let Table = {
 let Table = {
     handle_type: 'table',
     handle_type: 'table',
-    get_element: function (spec: { data: string[][], span: { [i: string]: { col: number, row: number } } }) {
+    get_element: function (spec: { data: any[][], span: { [i: string]: { col: number, row: number } } }) {
         const table_tpl = `
         const table_tpl = `
 <table>
 <table>
     <tr>
     <tr>
@@ -127,13 +127,13 @@ let Table = {
             for (let col_id in row) {
             for (let col_id in row) {
                 let data = spec.data[row_id][col_id];
                 let data = spec.data[row_id][col_id];
 
 
-                // 处理复合类型单元格,即单元格不是简单的html,而是一个output命令的spec
-                if (typeof data === 'object') {
-                    data = outputSpecToHtml(data);
+                // 处理简单类型单元格,即单元格不是output命令的spec
+                if (typeof data !== 'object') {
+                    data = {type: 'text', content: data, inline: true}
                 }
                 }
 
 
                 table_data[row_id].push({
                 table_data[row_id].push({
-                    data: data,
+                    data: outputSpecToHtml(data),
                     ...(spec.span[row_id + ',' + col_id] || {})
                     ...(spec.span[row_id + ',' + col_id] || {})
                 });
                 });
             }
             }