# In tasks.py file
from __future__ import absolute_import
from celery import shared_task
@shared_task
def add(x, y):
return x + y
# In views.py
def home(request):
context = {
'add': tasks.add.delay(5,2)
}
return render(request, "home.html", context)
# In home.html
<h1>{{ add }}</h1>
Run Code Online (Sandbox Code Playgroud)
home.html 上出现的不是 7,而是看起来像这样的胡言乱语:313e-44fb-818a-f2b2d824a46b。我该怎么做才能从我的 tasks.py 文件中获取数据?
我想使用supervisor在生产中运行芹菜,但我使用的是python 3而不是python 2.是否有支持python 3的主管?
另外,是否可以使用python 2为我的python 3代码运行cevisor的主管?
import pandas as pd
from scipy import misc
import numpy as np
import matplotlib.pyplot as plt
W = {'img':[misc.imread('pic.jpg')]}
df = pd.DataFrame(W)
# This displays the image
plt.imshow(df.img1[0])
plt.show()
df.to_csv('mypic.csv')
new_df= pd.read_csv('mypic.csv')
# This does not display the image
plt.imshow(new_df.img1[0])
plt.show()
Run Code Online (Sandbox Code Playgroud)
当我尝试将图像显示为由csv文件加载时,我得到错误:图像数据无法转换为浮点数.但是,我在使用数据帧时能够正确显示图像df
.
当我将df存储到csv文件时,我怀疑数据类型出了问题.我该如何解决这个问题?
编辑:我应该补充一点,我的主要目标是
from scipy import stats
import numpy as np
class your_distribution(stats.rv_continuous):
def _pdf(self, x):
p0 = 10.9949
p1 = 0.394447
p2 = 12818.4
p3 = 2.38898
return ((p1*p3)/(p3*p0+p2*p1))*((p0*np.exp(-1.0*p1*x))+(p2*np.exp(-1.0*p3*x)))
distribution = your_distribution(a=0.15, b=10.1)
sample = distribution.rvs(size=50000)
Run Code Online (Sandbox Code Playgroud)
The code above generates 50000 samples from a normalized pdf in the range 0.15 to 10.1. However, there is a disproportionately large number of samples generated at the upper bound b=10.1
. This does not make sense, as seen when the pdf is plotted.
How would …
I want the user to click a button on my website, and when it is clicked I want i = i + 1. So far I have this code that doesn't work
# On html file
<form method='get' action='#'>
<input type="submit" value="Next" name="Next"/>
</form>
# In django views
if request.GET.get('Name'):
print('user clicked summary')
Run Code Online (Sandbox Code Playgroud) 说我有一个字符串元素列表
wordlist = ["hi what's up home diddle mc doo", "Oh wise master kakarot", "hello have a da"]
Run Code Online (Sandbox Code Playgroud)
我希望列表中的每个元素最多包含3个单词或20个字符.有这个功能吗?
我有这个清单:
mylist = ["all dogs go", "why does one", "fell down so hard I"]
Run Code Online (Sandbox Code Playgroud)
是否有一个函数可以让我将单词添加"moo"
到所有元素的末尾mylist
?
我用 Django 和 React JS 制作了一个网站。我正在尝试将一个数组pict
从 JavaScript 前端传递到 Django(Python 后端)。
let pict = [];
pictureEl.addEventListener(`click`, function () {
console.log(`Take Pic`);
pict += webcam.snap();
console.log(pict);
});
Run Code Online (Sandbox Code Playgroud)
pict
是相机拍摄的图像数组。我如何将其传递到后端?
my_list = ["hi", "bye", "hi"]
Run Code Online (Sandbox Code Playgroud)
说我不知道是什么my_list
,我如何检查"bye"
我的列表中是否有元素.而如果"bye"
是一个元素my_list
,那我怎么才能知道它有哪些指数(这种情况"bye"
是my_list[1]
)?
我试图这样做,但它没有用.只是为了澄清我希望值等于列表[0]如果它存在.谢谢.
dictionary = {
try:
value : list[0],
except IndexError:
value = None
}
Run Code Online (Sandbox Code Playgroud) #In my views.py file
def home(request):
myfunc()
return render(request, "home.html", {})
Run Code Online (Sandbox Code Playgroud)
home.html 需要一段时间才能加载,因为每次单击它时都会执行 myfunc 函数。有没有办法在访问 home.html 之前将 myfunc 加载到服务器上,以便加载速度更快。是否可以每 15 分钟调用一次 myfunc,而不是每次访问 home.html 时调用一次?
python ×11
django ×4
list ×2
button ×1
celery ×1
csv ×1
dictionary ×1
function ×1
html ×1
if-statement ×1
javascript ×1
matplotlib ×1
pandas ×1
random ×1
reactjs ×1
scipy ×1
statistics ×1
supervisord ×1
try-catch ×1