如何使用 pandas 数据框和 ExcelWriter 对写入 Excel 文件的部分文本进行着色?
我已经设置了使用背景颜色和文本颜色以及不同内容来格式化单元格,但是您可以将格式设置仅应用于字符串“1, -3, 5”的单元格中的第一个逗号分隔值吗?
到目前为止,我有以下方法来格式化单个单元格,但我在文档中找不到如何将文本颜色应用于单元格中的部分文本,这可以使用 Microsoft Excel GUI 来完成。
import pandas as pd
excel_writer = pd.ExcelWriter("bob_saget.xlsx", engine='xlsxwriter')
sheet_name = "Full House"
data_frame = ...
headers = ...
data_frame.to_excel(excel_writer, sheet_name=sheet_name, index=False)
workbook = excel_writer.book
worksheet = excel_writer.sheets[sheet_name]
color_format = workbook.add_format({'bg_color': 'orange', 'border': True})
gray_cell_bg = workbook.add_format({'bg_color': '#d8d8d8'})
# using worksheet.write, re-writing the value in the cell and adding formatting
worksheet.write('B1', headers[1], color_format)
# hack solution to apply a background
worksheet.conditional_format("C4", {'type': 'text',
'criteria': 'not containing', …Run Code Online (Sandbox Code Playgroud) 当我使用 Seaborn 在同一轴上绘制多个线图时,不会创建任何图例。即使我在 sns.lineplot 中为图例提供参数“brief”或“full”,也不会显示任何内容,并且调用 ax.get_legend_handles_labels() 会返回两个空列表。
如何在右侧的框中添加图例,将线条的颜色链接到名称?
import seaborn as sns
import matplotlib.pyplot as plt
import random
fig1 = plt.figure(figsize=(12, 12))
ax = fig1.add_subplot(1, 1, 1)
x = range(10)
series = list()
for i in range(3):
y_i = list()
for j in range(10):
y_i.append(random.randint(0, 50))
sns.lineplot(x=x, y=y_i, ax=ax, legend='brief')
plt.show()
Run Code Online (Sandbox Code Playgroud) 在 Django 中,我使用rest_framework_simplejwt 应用程序设置了 JWT 身份验证。
我相信访问令牌的默认超时为 1 天,即使在 settings.py 中明确将其配置为 1 天后,令牌在大约 10 分钟后就不再起作用,并且服务器返回 401 响应。
设置.py:
REST_FRAMEWORK = {
'DEFAULT_PERMISSION_CLASSES': (
'rest_framework.permissions.IsAuthenticated',
),
'DEFAULT_AUTHENTICATION_CLASSES': (
'rest_framework_simplejwt.authentication.JWTAuthentication',
'rest_framework.authentication.SessionAuthentication',
),
}
SIMPLE_JWT = {
'TOKEN_LIFETIME': timedelta(days=1),
'TOKEN_REFRESH_LIFETIME': timedelta(days=7),
}
Run Code Online (Sandbox Code Playgroud)
我认为Django中的时间设置可能有问题,因此在settings.py中我放置了datetime.datetime.now()的打印,奇怪的是在startapp期间调用了两次,时差为2小时。不过,如果令牌生命周期应该是 1 天,它应该是有效的,所以我不确定问题是什么。
关于问题可能是什么的任何想法吗?预先非常感谢您。
python django jwt django-rest-framework django-rest-framework-simplejwt
我正在尝试按照 Django 文档中的描述为我的应用程序运行测试:https://docs.djangoproject.com/en/3.0/intro/tutorial05/
但是,通过下面显示的最小示例,我收到一条错误消息:
RuntimeError: Model class myproject.myapp.models.MyModel doesn't declare an explicit app_label and isn't in an application in INSTALLED_APPS.
我是否需要进行一些额外的配置才能运行测试,或者我可能已经犯过或忘记了一些其他错误?
完整的堆栈跟踪:
$ ./manage.py test myapp
System check identified no issues (0 silenced).
E
======================================================================
ERROR: myproject.myapp.tests (unittest.loader._FailedTest)
----------------------------------------------------------------------
ImportError: Failed to import test module: myproject.myapp.tests
Traceback (most recent call last):
File "/usr/lib64/python3.8/unittest/loader.py", line 436, in _find_test_path
module = self._get_module_from_name(name)
File "/usr/lib64/python3.8/unittest/loader.py", line 377, in _get_module_from_name
__import__(name)
File "/home/simernes/workspace/myproject/myapp/tests.py", line 2, in <module>
from .models import MyModel
File "/home/simernes/workspace/myproject/myapp/models.py", …Run Code Online (Sandbox Code Playgroud) 在 django 管理界面中,可以指定每个模型的权限。示例模型 Customer 的权限选项为:
然而,这些权限似乎不适用于 REST Framework API 视图 ( rest_framework.viewsets.ModelViewSet),实现Customer如下:
class CustomerViewSet(viewsets.ModelViewSet):
queryset = Customer.objects.all()
serializer_class = CustomerSerializer
class CustomerSerializer(serializers.ModelSerializer):
class Meta:
model = Customer
fields = '__all__'
Run Code Online (Sandbox Code Playgroud)
我认为通过将 DEFAULT_PERMISSION_CLASSES 设置为 DjangoModelPermissions 这些权限将得到反映,但事实并非如此:
REST_FRAMEWORK = {
'DEFAULT_PERMISSION_CLASSES': (
'rest_framework.permissions.DjangoModelPermissions',
),
'DEFAULT_AUTHENTICATION_CLASSES': (
'rest_framework_simplejwt.authentication.JWTAuthentication',
'rest_framework.authentication.SessionAuthentication',
),
}
Run Code Online (Sandbox Code Playgroud)
管理中定义的权限是否应该在视图中也适用于这些设置,是否不应该,和/或有什么方法可以实现这种情况?这样做的好处是系统管理员可以轻松地在管理界面中定义组并根据其问题区域定制权限,因此能够以这种方式定义权限是非常理想的。我已经看到了许多其他实现权限的方法,但从我所看到的来看,它们需要对 python 中的视图定义进行大量自定义。
版本:
这段代码:
use rayon::prelude::*; // 1.5.0
fn main() {
let mut items = Vec::new();
items.push("hello");
items.push("foo");
items.push("bar");
items.push("ipsum");
let mut counter = 0;
let results = items.par_iter().map(|item| {
// do something time consuming with item
counter += 1;
print!("completed {} items\r", counter);
0
});
}
Run Code Online (Sandbox Code Playgroud)
产生错误:
warning: unused variable: `item`
--> src/main.rs:12:41
|
12 | let results = items.par_iter().map(|item| {
| ^^^^ help: if this is intentional, prefix it with an underscore: `_item`
|
= note: `#[warn(unused_variables)]` on by default
warning: …Run Code Online (Sandbox Code Playgroud) python ×4
django ×3
django-rest-framework-simplejwt ×1
django-tests ×1
excel ×1
jwt ×1
matplotlib ×1
pandas ×1
rayon ×1
rust ×1
seaborn ×1