写入CSV适用于localhost。不适用于网站

时间:2017-09-20 02:06:26

标签: python flask

我创建了一个基本的Web应用程序,它接受输入并进行数学运算然后返回输出。我添加了一个函数来获取输入并将它们写入.csv文件。

在修补它之后,我完全按照我想要将其作为localhost运行的方式工作。我上传了新的应用程序和空白的.csv文件,但是每当我去运行应用程序时,它都不会加载结果页面,也没有任何内容写入.csv我甚至将相同的.csv文件放在多个位置(在静态中) ,模板和根文件夹),以防它没有找到我期望的地方

我还在学习python和flask并处理托管,因为我没有得到错误输出只是一个非加载的网页我不知道从哪里开始。想法?

这是我的代码:但正如我所说,它适用于本地主机。在我添加到.csv部分之前,网站工作正常 (注意缩进因为在这里粘贴它而关闭。它们在实践中是正确的)

@app.route('/', methods=['GET', 'POST'])
def step_bet():
    if request.method == 'POST':
        name = request.form['name']
        people_start = request.form['people_start']
        bet_amount = request.form['bet_amount']
        people_remain = request.form['people_remain']
        beta = request.form['beta']
        #form = web.input(name="nobody", people_start="null", bet="null", people_remain="null", beta="0")
        if people_start == None or bet_amount == None or people_remain == None:
            return render_template('error_form.html')
        else:
            people_startf = float(people_start)
            betf= float(bet_amount)
            people_remainf = float(people_remain)
            if beta == "Yes":
                cut = .125
            elif beta == "Members":
                cut = 0
            else:
                cut = .25       
            revenue = round((((people_startf * betf) * (1 - cut)) / people_remainf),2)
            if revenue < betf:
                revenue = betf
            profit = round((revenue - betf),2)
            people_remain_needed = int(((people_startf * betf) * (1 - cut))/betf)
            people_needed = int(people_remainf - people_remain_needed)
            if people_needed < 0:
                people_needed = 0
            else:
                pass
            # This array is the fields your csv file has and in the following code
            # you'll see how it will be used. Change it to your actual csv's fields.
            fieldnames = ['name', 'people_start', 'bet_amount', 'people_remain', 'beta', 'revenue', 'profit', 'people_needed']
            # We repeat the same step as the reading, but with "w" to indicate
            # the file is going to be written.
            # The second parameter with open is the mode, w is write, a is append. With append it automatically seeks to the end of the file.
            with open('step_bet_save.csv','a') as inFile:
                # DictWriter will help you write the file easily by treating the
                # csv as a python's class and will allow you to work with
                # dictionaries instead of having to add the csv manually.
                writer = csv.DictWriter(inFile, fieldnames=fieldnames)

                # writerow() will write a row in your csv file
                writer.writerow({'name': name, 'people_start': people_start, 'bet_amount': bet_amount, 'people_remain': people_remain, 'beta': beta, 'revenue': revenue, 'profit': profit, 'people_needed': people_needed})
            return render_template('results.html', revenue=revenue, profit=profit, name=name, people_needed=people_needed)
    else:
        return render_template('stepbet_form.html')

1 个答案:

答案 0 :(得分:0)

检查您对文件/文件夹的权限,并确保该文件夹具有写入权限。

如果您不能100%确定如何在基于* nix的Web服务器上执行此操作,请参阅CHMOD。