How to reuse models outside of py4web

120 views
Skip to first unread message

Jim Steil

unread,
Mar 11, 2021, 3:59:17 PM3/11/21
to py4web
Anyone have a way to access models defined in a py4web application outside of py4web?  I've tried various ways of building my app as a package an importing it but keep running into roadblocks.

Has anyone done this already and willing to share the solution?

Or, do I need to define my models outside of my py4web app and then import them into my app?

-Jim

Kevin Keller

unread,
Mar 11, 2021, 4:16:09 PM3/11/21
to Jim Steil, py4web
You mean like a apps that are basically separate apps that complement another app?

Like a plugin?



--
You received this message because you are subscribed to the Google Groups "py4web" group.
To unsubscribe from this group and stop receiving emails from it, send an email to py4web+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/py4web/a00131e3-6148-42eb-b5b7-687e2b882c87n%40googlegroups.com.

Jim Steil

unread,
Mar 11, 2021, 4:47:36 PM3/11/21
to py4web
No

I have a number of crontab scripts that run outside of py4web to do various tasks.  Automated reporting, notifications, synchronizing data, etc.

They all use the same database and I'm too lazy to maintain the models file in 2 separate places.

I may have figured it out.

I created a setup.py file in my apps directory and then ran

---
python3 setup.py sdist
pip3 install --upgrade dist/my_app_name-0.1.tar.gz --user
---

This installed the app as a package.  Now I can:

---
jim@pop-os:~/dev/py4web/apps$ python3
Python 3.8.6 (default, Jan 27 2021, 15:42:20) 
[GCC 10.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from my_app_name.lib.administration import AuthUser
>>> AuthUser.update_user_names()
---

My setup.py looks like this:

---
from setuptools import setup

setup(
    name="my_app_name",
    version="0.4",
    description="My App",
    url="my_github_repo_link",
    author="My Name",
    author_email="my email",
    license="MIT",
    packages=["my_app_name"],
    include_package_data=True,
    install_requires=[
        "dominate",
        "fdfgen",
        "googlemaps",
        "jinja2",
        "lxml",
        "openpyxl",
        "paramiko",
        "pycrypto",
        "pypyodbc",
        "pymysql",
        "pypdf2",
        "pysftp",
        "pysmb",
        "python-dateutil",
        "reportlab",
        "xlsxwriter",
        "xlrd",
        "xlwt",
        "zeep",
    ],
    zip_safe=False,
)
---

So, maybe I have it working...

-Jim

Jim Steil

unread,
Mar 11, 2021, 5:03:34 PM3/11/21
to py4web
Wait a minute, it isn't working.

It works only if I start the python3 repl in my apps directory (it wasn't importing the package, just the directory structure)

If I switch to another directory and try the same commands above I get :

ModuleNotFoundError: No module names 'my_app_name.controllers'

Which is coming from this line in the app root __init__.py

from .controllers import *

Dang it, back to the drawing board....

-Jim

Jim Steil

unread,
Mar 11, 2021, 5:10:56 PM3/11/21
to py4web
Ok, fixed that.

I changed setup.py to:

---
from setuptools import setup, find_packages()

setup(
    name="my_app_name",
    version="0.4",
    description="My App",
    url="my_github_repo_link",
    author="My Name",
    author_email="my email",
    license="MIT",
    packages=find_packages(),
---

Now it appears to be working.  Will be doing quite a bit more testing.

-Jim

Kevin Keller

unread,
Mar 12, 2021, 1:22:08 AM3/12/21
to Jim Steil, py4web
Awesome Jim.

What was your use case out of curiosity?

Thanks for sharing your progress here. 

Always helps. 

Jim Steil

unread,
Mar 12, 2021, 11:28:22 AM3/12/21
to py4web
Kevin

As I said before, primarily crontab jobs.

1 - data synchronization - we sync databases between IBM i DB2 and MySQL.  Primarily order data as orders come in, but also invoices, product database, bill of materials, etc.  Typical stuff for a manufacturing organization.
2 - Emailing standard reports - Build and email reports to employees or customers on a regular schedule
3 - Integration with partners - ftp, retrieving emails and processing attachments.

So, in our scenario I'll have a function within my py4web app to create a report on demand through the application.  Additionally, some users want to have that same report emailed to them say, every Friday morning.  So, I have a function to build the report and can call it through py4web or from a script run through crontab.

I think this is one of the things that drew me to py4web over Flask.  While it can be done with Flask and FlaskSQLAlchemy, you have to jump through hoops to create a context for the Flask app if you want to use parts of your flask app in other scripts.  py4web seemed cleaner and easier for me to wrap my head around.

-Jim

Reply all
Reply to author
Forward
0 new messages