Create config class (#8)
This commit is contained in:
parent
29e37350e5
commit
159e7949f1
@ -1,10 +1,10 @@
|
|||||||
"""Welcome to Pynecone! This file outlines the steps to create a basic app."""
|
"""Welcome to Pynecone! This file outlines the steps to create a basic app."""
|
||||||
import pcconfig
|
from pcconfig import config
|
||||||
|
|
||||||
import pynecone as pc
|
import pynecone as pc
|
||||||
|
|
||||||
docs_url = "https://pynecone.io/docs/getting-started/introduction"
|
docs_url = "https://pynecone.io/docs/getting-started/introduction"
|
||||||
filename = f"{pcconfig.APP_NAME}/{pcconfig.APP_NAME}.py"
|
filename = f"{config.app_name}/{config.app_name}.py"
|
||||||
|
|
||||||
|
|
||||||
class State(pc.State):
|
class State(pc.State):
|
||||||
@ -16,17 +16,22 @@ class State(pc.State):
|
|||||||
def index():
|
def index():
|
||||||
return pc.center(
|
return pc.center(
|
||||||
pc.vstack(
|
pc.vstack(
|
||||||
pc.heading("Welcome to Pynecone!"),
|
pc.heading("Welcome to Pynecone!", font_size="2em"),
|
||||||
pc.box("Get started by editing ", pc.code(filename)),
|
pc.box("Get started by editing ", pc.code(filename, font_size="1em")),
|
||||||
pc.link(
|
pc.link(
|
||||||
"Check out our docs!",
|
"Check out our docs!",
|
||||||
href=docs_url,
|
href=docs_url,
|
||||||
border="0.1em solid",
|
border="0.1em solid",
|
||||||
padding="0.5em",
|
padding="0.5em",
|
||||||
border_radius="0.5em",
|
border_radius="0.5em",
|
||||||
|
_hover={
|
||||||
|
"color": "rgb(107,99,246)",
|
||||||
|
},
|
||||||
),
|
),
|
||||||
|
spacing="1.5em",
|
||||||
|
font_size="2em",
|
||||||
),
|
),
|
||||||
padding="5em",
|
padding_top="10%",
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@ -6,6 +6,8 @@ Anything imported here will be available in the default Pynecone import as `pc.*
|
|||||||
from .app import App
|
from .app import App
|
||||||
from .base import Base
|
from .base import Base
|
||||||
from .components import *
|
from .components import *
|
||||||
|
from .config import Config
|
||||||
|
from .constants import Env
|
||||||
from .event import console_log, redirect, window_alert
|
from .event import console_log, redirect, window_alert
|
||||||
from .model import Model, session
|
from .model import Model, session
|
||||||
from .state import ComputedVar as var
|
from .state import ComputedVar as var
|
||||||
|
@ -215,15 +215,15 @@ class App(Base):
|
|||||||
def compile(self, ignore_env: bool = False):
|
def compile(self, ignore_env: bool = False):
|
||||||
"""Compile the app and output it to the pages folder.
|
"""Compile the app and output it to the pages folder.
|
||||||
|
|
||||||
If the pcconfig environment is set to production, the app will
|
If the config environment is set to production, the app will
|
||||||
not be compiled.
|
not be compiled.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
ignore_env: Whether to ignore the pcconfig environment.
|
ignore_env: Whether to ignore the config environment.
|
||||||
"""
|
"""
|
||||||
# Get the env mode.
|
# Get the env mode.
|
||||||
config = utils.get_config()
|
config = utils.get_config()
|
||||||
if not ignore_env and config.ENV != constants.Env.DEV.value:
|
if not ignore_env and config.env != constants.Env.DEV:
|
||||||
print("Skipping compilation in non-dev mode.")
|
print("Skipping compilation in non-dev mode.")
|
||||||
return
|
return
|
||||||
|
|
||||||
|
@ -6,13 +6,12 @@ from pynecone import constants
|
|||||||
from pynecone.utils import join
|
from pynecone.utils import join
|
||||||
|
|
||||||
# Template for the Pynecone config file.
|
# Template for the Pynecone config file.
|
||||||
PCCONFIG = f"""# The Pynecone configuration file.
|
PCCONFIG = f"""import pynecone as pc
|
||||||
|
|
||||||
APP_NAME = "{{app_name}}"
|
|
||||||
API_HOST = "http://localhost:8000"
|
config = pc.Config(
|
||||||
BUN_PATH = "$HOME/.bun/bin/bun"
|
app_name="{{app_name}}",
|
||||||
ENV = "{constants.Env.DEV.value}"
|
)
|
||||||
DB_URI = "sqlite:///{constants.DB_NAME}"
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
# Javascript formatting.
|
# Javascript formatting.
|
||||||
|
29
pynecone/config.py
Normal file
29
pynecone/config.py
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
from typing import Optional
|
||||||
|
|
||||||
|
from pynecone import constants
|
||||||
|
from pynecone.base import Base
|
||||||
|
|
||||||
|
|
||||||
|
class Config(Base):
|
||||||
|
"""A Pynecone config."""
|
||||||
|
|
||||||
|
# The name of the app.
|
||||||
|
app_name: str
|
||||||
|
|
||||||
|
# The backend API url.
|
||||||
|
api_url: str = "http://localhost:8000"
|
||||||
|
|
||||||
|
# The database url.
|
||||||
|
db_url: str = f"sqlite:///{constants.DB_NAME}"
|
||||||
|
|
||||||
|
# The redis url.
|
||||||
|
redis_url: Optional[str] = None
|
||||||
|
|
||||||
|
# The deploy url.
|
||||||
|
deploy_url: Optional[str] = None
|
||||||
|
|
||||||
|
# The environment mode.
|
||||||
|
env: constants.Env = constants.Env.DEV
|
||||||
|
|
||||||
|
# The path to the bun executable.
|
||||||
|
bun_path: str = "$HOME/.bun/bin/bun"
|
@ -134,5 +134,7 @@ class Endpoint(Enum):
|
|||||||
Returns:
|
Returns:
|
||||||
The full URL for the endpoint.
|
The full URL for the endpoint.
|
||||||
"""
|
"""
|
||||||
pcconfig = __import__(CONFIG_MODULE)
|
from pynecone import utils
|
||||||
return "".join([pcconfig.API_HOST, str(self)])
|
|
||||||
|
config = utils.get_config()
|
||||||
|
return "".join([config.api_url, str(self)])
|
||||||
|
@ -12,8 +12,8 @@ def get_engine():
|
|||||||
Returns:
|
Returns:
|
||||||
The database engine.
|
The database engine.
|
||||||
"""
|
"""
|
||||||
uri = utils.get_config().DB_URI
|
url = utils.get_config().db_url
|
||||||
return sqlmodel.create_engine(uri, echo=False)
|
return sqlmodel.create_engine(url, echo=False)
|
||||||
|
|
||||||
|
|
||||||
class Model(Base, sqlmodel.SQLModel):
|
class Model(Base, sqlmodel.SQLModel):
|
||||||
|
@ -87,11 +87,11 @@ def deploy(dry_run: bool = False):
|
|||||||
dry_run: Whether to run a dry run.
|
dry_run: Whether to run a dry run.
|
||||||
"""
|
"""
|
||||||
# Get the app config.
|
# Get the app config.
|
||||||
pcconfig = utils.get_config()
|
config = utils.get_config()
|
||||||
pcconfig.API_HOST = utils.get_production_backend_url()
|
config.api_url = utils.get_production_backend_url()
|
||||||
|
|
||||||
# Check if the deploy URI is set.
|
# Check if the deploy url is set.
|
||||||
if not hasattr(pcconfig, "DEPLOY_URI"):
|
if config.deploy_url is None:
|
||||||
typer.echo("This feature is coming soon!")
|
typer.echo("This feature is coming soon!")
|
||||||
typer.echo("Join our waitlist to be notified: https://pynecone.io/waitlist")
|
typer.echo("Join our waitlist to be notified: https://pynecone.io/waitlist")
|
||||||
return
|
return
|
||||||
@ -106,8 +106,8 @@ def deploy(dry_run: bool = False):
|
|||||||
return
|
return
|
||||||
|
|
||||||
# Deploy the app.
|
# Deploy the app.
|
||||||
data = {"userId": pcconfig.USERNAME, "projectId": pcconfig.APP_NAME}
|
data = {"userId": config.username, "projectId": config.app_name}
|
||||||
original_response = requests.get(pcconfig.DEPLOY_URI, params=data)
|
original_response = requests.get(config.deploy_url, params=data)
|
||||||
response = original_response.json()
|
response = original_response.json()
|
||||||
print("response", response)
|
print("response", response)
|
||||||
frontend = response["frontend_resources_url"]
|
frontend = response["frontend_resources_url"]
|
||||||
|
@ -36,6 +36,7 @@ from pynecone import constants
|
|||||||
|
|
||||||
if TYPE_CHECKING:
|
if TYPE_CHECKING:
|
||||||
from pynecone.components.component import ImportDict
|
from pynecone.components.component import ImportDict
|
||||||
|
from pynecone.config import Config
|
||||||
from pynecone.event import Event, EventHandler, EventSpec
|
from pynecone.event import Event, EventHandler, EventSpec
|
||||||
from pynecone.var import Var
|
from pynecone.var import Var
|
||||||
|
|
||||||
@ -233,14 +234,19 @@ def which(program: str) -> Optional[str]:
|
|||||||
return shutil.which(program)
|
return shutil.which(program)
|
||||||
|
|
||||||
|
|
||||||
def get_config() -> Any:
|
def get_config() -> Config:
|
||||||
"""Get the default pcconfig.
|
"""Get the app config.
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
The default pcconfig.
|
The app config.
|
||||||
"""
|
"""
|
||||||
sys.path.append(os.getcwd())
|
sys.path.append(os.getcwd())
|
||||||
return __import__(constants.CONFIG_MODULE)
|
try:
|
||||||
|
return __import__(constants.CONFIG_MODULE).config
|
||||||
|
except:
|
||||||
|
print(f"No {constants.CONFIG_MODULE} module found.")
|
||||||
|
print("Using default config.")
|
||||||
|
return Config(app_name="")
|
||||||
|
|
||||||
|
|
||||||
def get_bun_path():
|
def get_bun_path():
|
||||||
@ -249,7 +255,7 @@ def get_bun_path():
|
|||||||
Returns:
|
Returns:
|
||||||
The path to the bun executable.
|
The path to the bun executable.
|
||||||
"""
|
"""
|
||||||
return os.path.expandvars(get_config().BUN_PATH)
|
return os.path.expandvars(get_config().bun_path)
|
||||||
|
|
||||||
|
|
||||||
def get_app() -> Any:
|
def get_app() -> Any:
|
||||||
@ -259,7 +265,7 @@ def get_app() -> Any:
|
|||||||
The app based on the default config.
|
The app based on the default config.
|
||||||
"""
|
"""
|
||||||
config = get_config()
|
config = get_config()
|
||||||
module = ".".join([config.APP_NAME, config.APP_NAME])
|
module = ".".join([config.app_name, config.app_name])
|
||||||
app = __import__(module, fromlist=(constants.APP_VAR,))
|
app = __import__(module, fromlist=(constants.APP_VAR,))
|
||||||
return app
|
return app
|
||||||
|
|
||||||
@ -364,8 +370,8 @@ def get_production_backend_url() -> str:
|
|||||||
"""
|
"""
|
||||||
config = get_config()
|
config = get_config()
|
||||||
return constants.PRODUCTION_BACKEND_URL.format(
|
return constants.PRODUCTION_BACKEND_URL.format(
|
||||||
username=config.USERNAME,
|
username=config.username,
|
||||||
app_name=config.APP_NAME,
|
app_name=config.app_name,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@ -861,8 +867,10 @@ def get_redis():
|
|||||||
import redis
|
import redis
|
||||||
|
|
||||||
config = get_config()
|
config = get_config()
|
||||||
redis_host, redis_port = config.REDIS_HOST.split(":")
|
if config.redis_url is None:
|
||||||
print("Using redis at", config.REDIS_HOST)
|
return None
|
||||||
return redis.Redis(host=redis_host, port=redis_port, db=0)
|
redis_url, redis_port = config.redis_url.split(":")
|
||||||
|
print("Using redis at", config.redis_url)
|
||||||
|
return redis.Redis(host=redis_url, port=redis_port, db=0)
|
||||||
except:
|
except:
|
||||||
return None
|
return None
|
||||||
|
Loading…
Reference in New Issue
Block a user