osdir.com


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

creating a table within python code


Still no go.  I even tried using the entire path of the py ap in the SET
FLASK_APP command....

This is my code now after trying your suggestions.
===========================================================================
import os

from flask import Flask, session
from flask_session import Session
from sqlalchemy import create_engine
from sqlalchemy.orm import scoped_session, sessionmaker

app= Flask(__name__)

engine = create_engine(os.getenv("DATABASE_URL"))
db = scoped_session(sessionmaker(bind=engine))

@app.route("/")

def main():
    app.run()

    db.execute("CREATE TABLE books (id SERIAL PRIMARY KEY, isbn VARCHAR NOT
NULL, title VARCHAR NOT NULL, author INTEGER NOT NULL, year INTEGER NOT
NULL,)")
    db.execute("CREATE TABLE authors (id SERIAL PRIMARY KEY, name VARCHAR
NOT NULL,)")

    db.commit()

if __name__ == "__main__":
    main()
=================================================================================
Al (Buddy) Peacock, PMP, MCCT,  ITILv3, SMC, CSM, SPOC
(920) 740-3411
linkedin.com/in/buddypeacock <https://www.linkedin.com/in/buddypeacock/>




On Fri, May 22, 2020 at 11:04 AM Souvik Dutta <souvik.viksou at gmail.com>
wrote:

> And also you might have to add after the import statements app=
> Flask(__name__)
> and app.run() inside the main function
>
> On Fri, 22 May, 2020, 8:31 pm Souvik Dutta, <souvik.viksou at gmail.com>
> wrote:
>
>>  You might have to add @app.route("/") before the main method.
>>
>> On Fri, 22 May, 2020, 7:53 pm Buddy Peacock, <buddy.peacock at gmail.com>
>> wrote:
>>
>>> Thank you Souvik, but still having issues.  I have pasted the command
>>> line interaction this time.  My prior message is what appeared in the
>>> browser.
>>>
>>> ================================================================================================================
>>> c:\Harvard\project1>SET FLASK_APP="create_db.py"
>>>
>>> c:\Harvard\project1>set DATABASE_URL=postgres://
>>> guaqyugfujbudc:79ae65a6d8966991694906e4b96f20ebcfde5b80fb334e99d79d9300dd6ef95f at ec2-34-200-72-77.compute-1.amazonaws.com:5432/dep18tfh5g2eme
>>>
>>> c:\Harvard\project1>flask run
>>>  * Serving Flask app ""create_db.py"" (lazy loading)
>>>  * Environment: development
>>>  * Debug mode: on
>>>  * Restarting with stat
>>>  * Debugger is active!
>>>  * Debugger PIN: 124-607-194
>>>  * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
>>> 127.0.0.1 - - [22/May/2020 10:16:46] " [35m [1mGET / HTTP/1.1 [0m" 500 -
>>> Traceback (most recent call last):
>>>   File
>>> "C:\Users\buddy\AppData\Local\Programs\Python\Python38\Lib\site-packages\flask\cli.py",
>>> line 240, in locate_app
>>>     __import__(module_name)
>>> ModuleNotFoundError: No module named '"create_db'
>>>
>>> During handling of the above exception, another exception occurred:
>>>
>>> Traceback (most recent call last):
>>>   File
>>> "C:\Users\buddy\AppData\Local\Programs\Python\Python38\Lib\site-packages\flask\cli.py",
>>> line 338, in __call__
>>>     self._flush_bg_loading_exception()
>>>   File
>>> "C:\Users\buddy\AppData\Local\Programs\Python\Python38\Lib\site-packages\flask\cli.py",
>>> line 326, in _flush_bg_loading_exception
>>>     reraise(*exc_info)
>>>   File
>>> "C:\Users\buddy\AppData\Local\Programs\Python\Python38\Lib\site-packages\flask\_compat.py",
>>> line 39, in reraise
>>>     raise value
>>>   File
>>> "C:\Users\buddy\AppData\Local\Programs\Python\Python38\Lib\site-packages\flask\cli.py",
>>> line 314, in _load_app
>>>     self._load_unlocked()
>>>   File
>>> "C:\Users\buddy\AppData\Local\Programs\Python\Python38\Lib\site-packages\flask\cli.py",
>>> line 330, in _load_unlocked
>>>     self._app = rv = self.loader()
>>>   File
>>> "C:\Users\buddy\AppData\Local\Programs\Python\Python38\Lib\site-packages\flask\cli.py",
>>> line 388, in load_app
>>>     app = locate_app(self, import_name, name)
>>>   File
>>> "C:\Users\buddy\AppData\Local\Programs\Python\Python38\Lib\site-packages\flask\cli.py",
>>> line 250, in locate_app
>>>     raise NoAppException('Could not import
>>> "{name}".'.format(name=module_name))
>>> flask.cli.NoAppException: Could not import ""create_db.py"".
>>> 127.0.0.1 - - [22/May/2020 10:16:46] " [37mGET
>>> /?__debugger__=yes&cmd=resource&f=style.css HTTP/1.1 [0m" 200 -
>>> 127.0.0.1 - - [22/May/2020 10:16:46] " [37mGET
>>> /?__debugger__=yes&cmd=resource&f=debugger.js HTTP/1.1 [0m" 200 -
>>> 127.0.0.1 - - [22/May/2020 10:16:46] " [37mGET
>>> /?__debugger__=yes&cmd=resource&f=jquery.js HTTP/1.1 [0m" 200 -
>>> 127.0.0.1 - - [22/May/2020 10:16:46] " [37mGET
>>> /?__debugger__=yes&cmd=resource&f=console.png HTTP/1.1 [0m" 200 -
>>> 127.0.0.1 - - [22/May/2020 10:16:46] " [37mGET
>>> /?__debugger__=yes&cmd=resource&f=ubuntu.ttf HTTP/1.1 [0m" 200 -
>>> 127.0.0.1 - - [22/May/2020 10:16:46] " [37mGET
>>> /?__debugger__=yes&cmd=resource&f=console.png HTTP/1.1 [0m" 200 -
>>>
>>> Al (Buddy) Peacock, PMP, MCCT,  ITILv3, SMC, CSM, SPOC
>>> (920) 740-3411
>>> linkedin.com/in/buddypeacock <https://www.linkedin.com/in/buddypeacock/>
>>>
>>>
>>>
>>>
>>> On Fri, May 22, 2020 at 9:42 AM Souvik Dutta <souvik.viksou at gmail.com>
>>> wrote:
>>>
>>>> There will be quotes when doing FLASK_APP="<your_app_name>" I think
>>>> that should solve the problem.
>>>>
>>>> On Fri, 22 May, 2020, 5:35 pm Buddy Peacock, <buddy.peacock at gmail.com>
>>>> wrote:
>>>>
>>>>> I'm working on my first python project in CS50W  and I am trying to
>>>>> create
>>>>> 2 tables.
>>>>> I am getting the following error when trying to run it:  I have
>>>>> included my
>>>>> code below the error message.
>>>>>
>>>>> flask.cli.NoAppException: Failed to find Flask application or factory
>>>>> in
>>>>> module "create_db". Use "FLASK_APP=create_db:name to specify one.
>>>>> Traceback (most recent call last)
>>>>> File
>>>>>
>>>>> "C:\Users\buddy\AppData\Local\Programs\Python\Python38\Lib\site-packages\flask\_compat.py",
>>>>> line 39, in reraise
>>>>> raise value
>>>>> File
>>>>>
>>>>> "C:\Users\buddy\AppData\Local\Programs\Python\Python38\Lib\site-packages\flask\cli.py",
>>>>> line 97, in find_best_app
>>>>> raise NoAppException(
>>>>> flask.cli.NoAppException: Failed to find Flask application or factory
>>>>> in
>>>>> module "create_db". Use "FLASK_APP=create_db:name to specify one.
>>>>>
>>>>> I used:
>>>>> FLASK_APP=create_db.py   at the command line
>>>>>
>>>>> and this is my code:
>>>>>
>>>>> import os
>>>>> from flask import Flask, session
>>>>> from flask_session import Session
>>>>> from sqlalchemy import create_engine
>>>>> from sqlalchemy.orm import scoped_session, sessionmaker
>>>>> engine = create_engine(os.getenv("DATABASE_URL"))
>>>>> db = scoped_session(sessionmaker(bind=engine))
>>>>> def main():
>>>>>     db.execute("CREATE TABLE books (id SERIAL PRIMARY KEY, isbn
>>>>> VARCHAR NOT
>>>>> NULL, title VARCHAR NOT NULL, author INTEGER NOT NULL, year INTEGER NOT
>>>>> NULL,)")
>>>>>     db.execute("CREATE TABLE authors (id SERIAL PRIMARY KEY, name
>>>>> VARCHAR
>>>>> NOT NULL,)")
>>>>>     db.commit()
>>>>> if __name__ == "__main__":
>>>>>     main()
>>>>>
>>>>> Does anyone have any ideas?
>>>>>
>>>>> Al (Buddy) Peacock, PMP, MCCT,  ITILv3, SMC, CSM, SPOC
>>>>> (920) 740-3411
>>>>> linkedin.com/in/buddypeacock <
>>>>> https://www.linkedin.com/in/buddypeacock/>
>>>>> --
>>>>> https://mail.python.org/mailman/listinfo/python-list
>>>>>
>>>>