我被迫将Python2.6用于CGI项目,并尝试使用Plotly创建直方图。
这是创建直方图的函数:
def get_histogram(self, dataset_uuid, target, title, xaxisTitle, yaxisTitle):
max_threshold = self.get_max_threshold(dataset_uuid, target)
thresholds = [0.01, 0.05, 0.1, 0.15, 0.2, 0.25, 0.3, 0.35, 0.4, 0.45, 0.5]
counts = []
for i, percentage in enumerate(thresholds):
# Query the database for all of the records with frequencies above the
# threshold.
result = (self.session.query(Mesh)
.filter(Mesh.frequency >= percentage * max_threshold)
)
# Appends the fetched record count to the list of counts
counts.append(result.count())
# Change percentages from float to integer
thresholds[i] = percentage * 100
graph = go.Histogram(
x = thresholds,
y = counts,
type='bar'
)
layout = go.Layout(
title = title,
xaxis = dict(
title = xaxisTitle
),
yaxis = dict(
title = yaxisTitle
)
)
data = [graph]
figure = go.Figure(data = data, layout = layout)
return json.dumps(figure, cls=plotly.utils.PlotlyJSONEncoder)
在Python2.7下,我的脚本按预期运行,但是,当我尝试2.6时,我收到以下语法错误:
Traceback (most recent call last):
File "index.cgi", line 4, in <module>
from viads import Application
File "/Library/WebServer/Documents/viads/viads/__init__.py", line 3, in <module>
from application import Application
File "/Library/WebServer/Documents/viads/viads/application.py", line 8, in <module>
from mysqlDatabase import MysqlDatabase
File "/Library/WebServer/Documents/viads/viads/mysqlDatabase.py", line 3, in <module>
from database import Database
File "/Library/WebServer/Documents/viads/viads/database.py", line 9, in <module>
import plotly.plotly
File "/Library/Python/2.6/site-packages/plotly/__init__.py", line 31, in <module>
from plotly import (plotly, graph_objs, grid_objs, tools, utils, session,
File "/Library/Python/2.6/site-packages/plotly/plotly/__init__.py", line 10, in <module>
from . plotly import (
File "/Library/Python/2.6/site-packages/plotly/plotly/plotly.py", line 114
user_plot_options = {k: v for k, v in user_plot_options.items()
^
SyntaxError: invalid syntax
调查plotly.py
显示以下列表理解:
user_plot_options = {k: v for k, v in user_plot_options.items()
if k in default_plot_options}
我认为我试图将双打列表转换为Plotly配置选项字典。
由于这是Python2.6的Plotly源本身内部的语法错误,我应该为自己的项目修补此问题并考虑将其归还给Plotly吗?
P.S。我理解使用旧版本Python和不使用FCGI的缺点