具有 Plotly/python 数据框的语言环境库适用于 jupyter 但不适用于 Flask/dash

locale library with Plotly/python dataframe works with jupyter but not with Flask/dash

我正在使用 Jupyter notebook 处理 COVID 数据,其中我使用我的语言环境转换了日期格式,效果很好。我使用了以下代码并能够添加一个带有语言环境日期的列。

import locale
locale.setlocale(locale.LC_TIME, "ta_TA.utf8")
df_state = res[res['state']=='Tamil Nadu']
df_state_ser = df_state.copy() 
df_state_ser.drop(['district'],axis=1,inplace=True)
df_state_ser.reset_index(inplace=True)
df_state_ser.drop(['active'],axis=1,inplace=True)
df_state_ser.drop(['notes'],axis=1,inplace=True)
df_state_ser.drop(['index'],axis=1,inplace=True)
#df_state_ser.drop(['state'],axis=1,inplace=True)
df_state_ser.sort_values(by='date', ascending = False)
df_state_ser = df_state_ser.groupby(['date']).sum().reset_index()
df_state_ser['date'] = pd.to_datetime(df_state_ser['date'])
df_state_ser['tamil'] = df_state_ser['date'].dt.strftime("%d %b %y")
#df_state_ser.rename(columns={df_state_ser.columns[0]:'தேதி'}, inplace=True)
df_state_ser.rename(columns={df_state_ser.columns[1]:'உறுதிச்செய்யப்பட்டவை'}, inplace=True)
df_state_ser.rename(columns={df_state_ser.columns[3]:'குணமடைந்தவர்கள்'}, inplace=True)
df_state_ser.rename(columns={df_state_ser.columns[2]:'இறந்தவர்கள்'}, inplace=True)
df_state_ser.rename(columns={df_state_ser.columns[4]:'தேதி'}, inplace=True)
df_state_ser

附图给出了数据帧的输出。突出显示的第一个日期列已根据最后一列转换为语言环境日期格式

我能够使用数据框并使用我的语言环境日期格式获取绘图,效果也很好。

import plotly.graph_objs as go
import plotly.express as px
df_long_TN=pd.melt(df_state_ser, id_vars=['தேதி'], value_vars=['உறுதிச்செய்யப்பட்டவை','இறந்தவர்கள்','குணமடைந்தவர்கள்'])
fig = px.scatter( df_long_TN,x='தேதி', y="value", color="variable")
fig.update_traces(hovertemplate=None)
fig.update_layout(height=500, width= 605,font=dict(size=10,color="white"),xaxis_title="தேதி",paper_bgcolor= "#2D2D2D",plot_bgcolor="#2D2D2D",
         yaxis=dict(tickformat=".f"),     xaxis=dict(tickangle=270),      legend=dict(x=0,y=1.3,traceorder="normal",font=dict(family="sans-serif",size=12)),
        xaxis_showgrid=False, yaxis_showgrid=False,  legend_title_text= 'மொத்தம்')
fig.show()

根据下图对代码进行了更改,并且能够毫无问题地获取带有区域设置日期的图表。

如果我在 Flask 应用程序中使用上面和 运行 相同的代码,我不会在图表中获取语言环境日期。我得到如下所述的输出。

需要你的 inputs/pointer 来修复 Flask 应用程序中的相同问题。

从 locale.setlocale(locale.LC_TIME, "ta_TA.utf8") 改为 locale.setlocale(category=locale.LC_ALL,locale="Tamil") 修复了 flask

的问题