Вопрос

im unable to get access to the peewee database from a python script other than the one writing the data (but weirdly in the interactive shell). I'm developing a data mining application with python,flask,and peewee. I'm going to cut it up into a byte size problem here, but it is large in scope than what im presenting. all python files are located in the same folder

the basic process is a python command line operation that goes out and grabs some information from the new york times and instagram and stores them into Peewee (sqllite) database. I then use a flask application to explore the data.

heres one of the models from database.py:

from peewee import *

class Story(Model):
    title = CharField()
    story = TextField()
    date = DateField()
    class Meta:
        database = SqliteDatabase("newsalmost.db",threadlocals = True)

newsalmost looks something like this:

from database import *

class NewsAlmost(object):
    def __init__(self):
        self.db = SqliteDatabase("newsalmost.db",threadlocals = True)

if does stuff like this:

story = Story.create(title = self.feed.stories[key]["title"], story = self.feed.stories[key],date = datetime.datetime.now(), is_relative = True)

i can then run:

"python newslamost.py -g" 

and it will go gather stuff and write them to the database

i then have a file called webapp.py, which is a flask app

import newsalmost
from flask import Flask
from flask import send_file
import subprocess
app = Flask(__name__)
import os
import json
from database import *

@app.route("/")
def index():
    r = []
    for i in Image.select():
        r.append(str(i))

    return json.dumps(r)

"python webapp.py"

I've tried to strip it down to the core problem. The flask app never sees anything in the database.. ever..

i KNOW its logging them correctly, because i can actually run "python" in that folder, import database and i get many stories from Stories.select()

the WEIRDER thing is that i initially had architected this in a more desirable way where the flask app merely creates a new instance of the newsalmost instance then calls functions on that to return stuff from the database and this worked.. IN DEV MODE. But when i deployed it to my web faction server (and got everything up there running), i am again greeted always with an empty response from the database. this is my attempt to try and directly reference the database in flask, thinking that maybe it was newsalmost fucking things up.. but nope.

I'm just bewildered why an sqllite database would perform exactly as expected locally, but not once deployed to a webserver, but also... why does the flask code i provided not get anything from the database, but running the same database query in the interactive shell work?

any ideas?

Это было полезно?

Решение

I don’t know if that would solve your problem, but you should use the same database object in both modules:

database.py:

from peewee import *

db = SqliteDatabase("newsalmost.db",threadlocals = True)

class Story(Model):
    title = CharField()
    story = TextField()
    date = DateField()
    class Meta:
        database = db

newsalmost.py:

from database import *

class NewsAlmost(object):
    def __init__(self):
    self.db = db
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top