小编eus*_*ass的帖子

将Matplotlib绘图图像保存到Django模型中

出于学习目的,我正在制作一个使用matplotlib制作绘图的webapp,我想将该绘图的图像保存到figurePlot模型的字段中,但是当我创建一个绘图时,它会将空白图像保存到/media/figures/目录中.我按照其他帖子的建议做了这个,但它没有用.

更多信息 当我制作绘图时,绘图图像会保存在我的django项目的主目录中,其名称类似于<_io.StringIO object at 0xb1ac69ec>,但正如我所说,保存在模型中的绘图图像是空白的.如果重要的话,我也使用Python 2.7.

def simple_plot(request):
    if request.method == "POST":
        name = request.POST.get("name")
        xvalues = request.POST.get("xvalues")
        yvalues = request.POST.get("yvalues")
        plots.simple_plot(request, name, xvalues, yvalues)
        messages.success(request, "Plot created!")
        redirect("plots:create")
    return render(request, 'plots/simple.html')
Run Code Online (Sandbox Code Playgroud)

绘制的文件和创建的绘图实例.

def simple_plot(request ,name, xvalues, yvalues):
    file_name = name+".png"
    xvalues = [int(x.replace(" ","")) for x in xvalues.split(",")]
    yvalues = [int(y.replace(" ","")) for y in yvalues.split(",")]

    figure = io.StringIO()
    plot = plt.plot(xvalues, yvalues)
    plt.savefig(u'%s' % figure, format="png")

    content_file = ContentFile(figure.getvalue()) …
Run Code Online (Sandbox Code Playgroud)

python django plot matplotlib

8
推荐指数
1
解决办法
1432
查看次数

PyQt5:Gtk-CRITICAL**:IA__gtk_widget_style_get:断言'GTK_IS_WIDGET(widget)'失败

我用PyQt5得到这个错误,我想我只有在使用QTextEdit时才会出现此错误.我尝试在QTextEdit的位置使用QLineEdit,它没有问题.当我打开程序时使用QTextEdit我得到了该错误,程序运行速度非常慢.错误:(python:4843): Gtk-CRITICAL **: IA__gtk_widget_style_get: assertion 'GTK_IS_WIDGET (widget)' failed

import sys
from PyQt5.QtWidgets import (QWidget, QLabel, QLineEdit, 
    QTextEdit, QGridLayout, QApplication)


class Example(QWidget):

    def __init__(self):
        super(Example, self).__init__()

        self.initUI()


    def initUI(self):

        title = QLabel('Title')
        author = QLabel('Author')
        review = QLabel('Review')

        titleEdit = QLineEdit()
        authorEdit = QLineEdit()
        reviewEdit = QTextEdit()

        grid = QGridLayout()
        grid.setSpacing(10)

        grid.addWidget(title, 1, 0)
        grid.addWidget(titleEdit, 1, 1)

        grid.addWidget(author, 2, 0)
        grid.addWidget(authorEdit, 2, 1)

        grid.addWidget(review, 3, 0)
        grid.addWidget(reviewEdit, 3, 1, 5, 1)

        self.setLayout(grid) 

        self.setGeometry(300, 300, 350, 300)
        self.setWindowTitle('Review')   
        self.show()


if __name__ …
Run Code Online (Sandbox Code Playgroud)

python qt pyqt qt5 pyqt5

5
推荐指数
1
解决办法
1510
查看次数

Rails Rspec - 未定义的方法`session'

我测试我的会话控制器,但我得到这个错误,登录功能工作,我在浏览器上测试它,但我是新的测试Rspec,无法让它工作

 Failure/Error: expect(response.session[:user_id]).to eq(@user_attr.id)
 NoMethodError:
   undefined method `session' for #<ActionController::TestResponse:0xd30df10>
 # ./spec/controllers/sessions_controller_spec.rb:20:in `block (3 levels) in <top (required)>'
Run Code Online (Sandbox Code Playgroud)

这是我的控制器的代码:

def new
    @user = User.new
end

def create
    @user = User.find_by(username: params[:user][:username])
    if @user && @user.authenticate(params[:user][:password])
        session[:user_id] = @user.id
        redirect_to root_path
    else
        render :new
    end
end
Run Code Online (Sandbox Code Playgroud)

Rspec代码:

require 'rails_helper'

RSpec.describe SessionsController, type: :controller do
describe "get Login page" do
    it "returns http status" do
        get :new
        expect(response).to have_http_status(:success)
    end
end

describe "session" do
    before(:each) do
        @user = FactoryGirl.create(:user)
        @user_attr = FactoryGirl.attributes_for(:user) …
Run Code Online (Sandbox Code Playgroud)

ruby session rspec ruby-on-rails

3
推荐指数
1
解决办法
1443
查看次数

How to check if user input is a number

I'm making this small program and I want to check if the user entered an Integer number, if he did it the program will continue but if the user enters a string I want to the program to ask the user to enter an Integer until he does it, here's my code snippet:

print "How old are you:"
    user_age = gets.chomp.to_i
    if user_age.is_a? String
        puts "Please enter an integer number:"
        user_age = gets.chomp.to_i
        until user_age.is_a? Numeric
            puts "Please enter an …
Run Code Online (Sandbox Code Playgroud)

ruby

1
推荐指数
1
解决办法
4981
查看次数

Django'对称'解释

我不明白Django的"对称"是如何工作的,我阅读了关于它的文档部分,但它太短了并且搜索了它但我并不是真的理解它...任何人都可以解释对称和非对称关系之间的区别更详细一点?

django django-models

1
推荐指数
1
解决办法
82
查看次数

标签 统计

django ×2

python ×2

ruby ×2

django-models ×1

matplotlib ×1

plot ×1

pyqt ×1

pyqt5 ×1

qt ×1

qt5 ×1

rspec ×1

ruby-on-rails ×1

session ×1