生成的HTML代码中的空白字段(Facet概述)



所以我在为方面概述生成html代码时遇到了一个wierd错误。

当我对类或函数之外的所有内容进行编码时,代码就是这样的:

# create a "jsonable" DF to make a Facet Dive
IO_train = X_train.merge(y_train, left_index=True, right_index=True)
IO_test = X_test.merge(y_test, left_index=True, right_index=True)
# facet analyse, not facet dive
gfgs = GenericFeatureStatisticsGenerator()
proto = gfgs.ProtoFromDataFrames([{"name": "train",
"table": IO_train},
{"name": "test",
"table": IO_test}])
#für facet analyse ins utf-8 format bringen
protostr = base64.b64encode(proto.SerializeToString()).decode("utf-8")
# create html template
HTML_TEMPLATE_ANALYSIS = """
<script src="https://cdnjs.cloudflare.com/ajax/libs/
webcomponentsjs/1.3.3/webcomponents-lite.js"></script>
<link rel="import" href="https://raw.githubusercontent.com/
PAIR-code/facets/1.0.0/facets-dist/facets-jupyter.html" >
<facets-overview id="elem3"></facets-overview>
<script>
document.querySelector("#elem3").protoInput = "{protostr}";
</script>"""
html_analysis = HTML_TEMPLATE_ANALYSIS.format(protostr=protostr)

它创建了一个完美的html代码:

# html_analysis
'n <script src="https://cdnjs.cloudflare.com/ajax/libs/nwebcomponentsjs/1.3.3/webcomponents- 
lite.js"></script>n <link rel="import" href="https://raw.githubusercontent.com/nPAIR- 
code/facets/1.0.0/facets-dist/facets-jupyter.html" >n <facets-overview id="elem3"></facets- 
overview>n <script>n document.querySelector("#elem3").protoInput ......

但是当我尝试使用类和函数时:

class Facet:
# at first create a jsonable DF to make a Facet Overview
def crt_IO_json(self, training_features, training_output, test_features, test_output):
self.training_features = training_features
self.training_output = training_output
self.test_features = test_features
self.test_output = test_output
IO_train = training_features.merge(training_output, left_index=True, right_index=True)
IO_test = test_features.merge(test_output, left_index=True, right_index=True)
return IO_train, IO_test

def Overview(self, IO_train, IO_test):
gfgs = GenericFeatureStatisticsGenerator()
proto = gfgs.ProtoFromDataFrames([{"name": "train",
"table": IO_train},
{"name": "test",
"table": IO_test}])
# für facet Overview ins utf-8 Format konvertieren
protostr = base64.b64encode(proto.SerializeToString()).decode("utf-8")

#HTML Template erstellen
#Das Html template erstellen
HTML_TEMPLATE_ANALYSIS = """
<script src="https://cdnjs.cloudflare.com/ajax/libs/
webcomponentsjs/1.3.3/webcomponents-lite.js"></script>
<link rel="import" href="https://raw.githubusercontent.com/
PAIR-code/facets/1.0.0/facets-dist/facets-jupyter.html" >
<facets-overview id="elemOverview"></facets-overview>
<script>
document.querySelector("#elemOverview").protoInput = "{protostr}";
</script>"""
html_analysis = HTML_TEMPLATE_ANALYSIS.format(protostr=protostr)
return html_analysis
# Show unprocessed data
Splitter = Split()
a, b, c, d = Splitter.IO(df_Machine)
l = Facet()
h, j = l.crt_IO_json(a, c, b, d)
html_analysis = l.Overview(h, j)

​它创建了一个带有空格的html字符串:

html_analysis
'n         <script src="https://cdnjs.cloudflare.com/ajax/libs/n        
webcomponentsjs/1.3.3/webcomponents-lite.js"></script>n         <link rel="import" 
href="https://raw.githubusercontent.com/n        PAIR-code/facets/1.0.0/facets-dist/facets- 
jupyter.html" >n         <facets-overview id="elemOverview"></facets-overview>n         <script>n         
document.querySelector("#elemOverview").protoInput = "CrBrCgV0cmFpbhDM2QcaxQcKC3hfTFZEVF9NZWFuEAEas

如果代码不可读,我会尝试调整代码,提前感谢

我在类外创建了HTML_TEMPLATE,它现在可以工作了:

HTML_TEMPLATE_ANALYSIS = """
<script src="https://cdnjs.cloudflare.com/ajax/libs/
webcomponentsjs/1.3.3/webcomponents-lite.js"></script>
<link rel="import" href="https://raw.githubusercontent.com/
PAIR-code/facets/1.0.0/facets-dist/facets-jupyter.html" >
<facets-overview id="elemOverview"></facets-overview>
<script>
document.querySelector("#elemOverview").protoInput = "{protostr}";
</script>"""
class Facet:
# at first create a jsonable DF to make a Facet Overview
def crt_IO_json(self, training_features, training_output, test_features, 
test_output):
self.training_features = training_features
self.training_output = training_output
self.test_features = test_features
self.test_output = test_output
IO_train = training_features.merge(training_output, left_index=True, 
right_index=True)
IO_test = test_features.merge(test_output, left_index=True, 
right_index=True)
return IO_train, IO_test
def Overview(self, IO_train, IO_test):
gfgs = GenericFeatureStatisticsGenerator()
proto = gfgs.ProtoFromDataFrames([{"name": "train",
"table": IO_train},
{"name": "test",
"table": IO_test}])
# für facet Overview ins utf-8 Format konvertieren
protostr = base64.b64encode(proto.SerializeToString()).decode("utf-8")
return html_analysis
# Show unprocessed data
Splitter = Split()
a, b, c, d = Splitter.IO(df_Machine)
l = Facet()
h, j = l.crt_IO_json(a, c, b, d)
html_analysis = l.Overview(h, j)

最新更新