From 6c2f4deb70d413207e7b3442656209441a720ed5 Mon Sep 17 00:00:00 2001 From: Wazarr <39807740+Wazarr94@users.noreply.github.com> Date: Tue, 31 Jan 2023 05:38:48 +0100 Subject: [PATCH] Make db_url optional (#402) --- docker-example/README.md | 9 +++++++-- pynecone/app.py | 3 ++- pynecone/config.py | 2 +- pynecone/model.py | 5 +++++ 4 files changed, 15 insertions(+), 4 deletions(-) diff --git a/docker-example/README.md b/docker-example/README.md index 6870cec3d..ada68bdde 100644 --- a/docker-example/README.md +++ b/docker-example/README.md @@ -1,10 +1,13 @@ # Pynecone Container Image Build + This example describes how to create and use a container image for Pynecone with your own code. ## Update Requirements + The `requirements.txt` includes the pynecone-io package which is need to install Pynecone framework. If you use additional packages in your project you have add this in the `requirements.txt` first. Copy the `Dockerfile` and the `requirements.txt` file in your project folder. ## Customize Pynecone Config + The `pcconfig.py` includes the configuration of your Pynecone service. Edit the file like the following configuration. If you want to use a custom database you can set the endpoint in this file. ```python @@ -18,7 +21,8 @@ config = pc.Config( ) ``` -## Build Pyonecone Container Image +## Build Pynecone Container Image + To build your container image run the following command: ```bash @@ -26,8 +30,9 @@ docker build -t pynecone-project:latest . ``` ## Start Container Service + Finally, you can start your Pynecone container service as follows: ```bash docker run -d -p 3000:3000 -p 8000:8000 --name pynecone pynecone:latest -``` \ No newline at end of file +``` diff --git a/pynecone/app.py b/pynecone/app.py index 04656c945..ef70c0860 100644 --- a/pynecone/app.py +++ b/pynecone/app.py @@ -302,7 +302,8 @@ class App(Base): return # Create the database models. - Model.create_all() + if config.db_url is not None: + Model.create_all() # Empty the .web pages directory compiler.purge_web_pages_dir() diff --git a/pynecone/config.py b/pynecone/config.py index db5fa4c89..7bd905d7c 100644 --- a/pynecone/config.py +++ b/pynecone/config.py @@ -22,7 +22,7 @@ class Config(Base): api_url: str = constants.API_URL # The database url. - db_url: str = constants.DB_URL + db_url: Optional[str] = constants.DB_URL # The redis url. redis_url: Optional[str] = None diff --git a/pynecone/model.py b/pynecone/model.py index a53e48432..f2adfdf80 100644 --- a/pynecone/model.py +++ b/pynecone/model.py @@ -11,8 +11,13 @@ def get_engine(): Returns: The database engine. + + Raises: + ValueError: If the database url is None. """ url = utils.get_config().db_url + if url is None: + raise ValueError("No database url in config") return sqlmodel.create_engine(url, echo=False)