Skip to content

Commit a66fa3f

Browse files
cpsievertwch
andauthored
Follow up to posit-dev#1508: move dotenv complexity to app_utils.py module (posit-dev#1515)
Co-authored-by: Winston Chang <[email protected]>
1 parent ef0f9f6 commit a66fa3f

File tree

20 files changed

+194
-28
lines changed

20 files changed

+194
-28
lines changed

examples/chat/enterprise/aws-bedrock-anthropic/app.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,17 @@
44
# To get started, follow the instructions at https://aws.amazon.com/bedrock/claude/
55
# as well as https://github.com/anthropics/anthropic-sdk-python#aws-bedrock
66
# ------------------------------------------------------------------------------------
7-
from pathlib import Path
8-
97
from anthropic import AnthropicBedrock
10-
from dotenv import load_dotenv
118

129
from shiny.express import ui
1310

11+
# In Shiny Core, do `from app_utils import load_dotenv`
12+
from .app_utils import load_dotenv
13+
1414
# Either explicitly set the AWS environment variables before launching the app, or set
1515
# them in a file named `.env`. The `python-dotenv` package will load `.env` as
1616
# environment variables which can be read by `os.getenv()`.
17-
_ = load_dotenv(Path(__file__).parent / ".env")
18-
17+
load_dotenv()
1918
llm = AnthropicBedrock(
2019
# aws_secret_key=os.getenv("AWS_SECRET_KEY"),
2120
# aws_access_key=os.getenv("AWS_ACCESS_KEY"),
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import os
2+
from pathlib import Path
3+
4+
app_dir = Path(__file__).parent
5+
env_file = app_dir / ".env"
6+
7+
8+
def load_dotenv(dotenv_path: os.PathLike[str] = env_file, **kwargs) -> None:
9+
"""
10+
A convenience wrapper around `dotenv.load_dotenv` that warns if `dotenv` is not installed.
11+
It also returns `None` to make it easier to ignore the return value.
12+
"""
13+
try:
14+
import dotenv
15+
16+
dotenv.load_dotenv(dotenv_path=dotenv_path, **kwargs)
17+
except ImportError:
18+
import warnings
19+
20+
warnings.warn(
21+
"Could not import `dotenv`. If you want to use `.env` files to "
22+
"load environment variables, please install it using "
23+
"`pip install python-dotenv`.",
24+
stacklevel=2,
25+
)
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
shiny
2+
python-dotenv
3+
tokenizers
4+
anthropic

examples/chat/enterprise/azure-openai/app.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,8 @@
44
# To get setup, follow the instructions at https://learn.microsoft.com/en-us/azure/ai-services/openai/quickstart?tabs=command-line%2Cpython-new&pivots=programming-language-python#create-a-new-python-application
55
# ------------------------------------------------------------------------------------
66
import os
7-
from pathlib import Path
87

9-
import dotenv
8+
from app_utils import load_dotenv
109
from openai import AzureOpenAI
1110

1211
from shiny.express import ui
@@ -15,7 +14,7 @@
1514
# variables before launching the app, or set them in a file named `.env`. The
1615
# `python-dotenv` package will load `.env` as environment variables which can later be
1716
# read by `os.getenv()`.
18-
dotenv.load_dotenv(Path(__file__).parent / ".env")
17+
load_dotenv()
1918

2019
llm = AzureOpenAI(
2120
api_key=os.getenv("AZURE_OPENAI_API_KEY"),
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import os
2+
from pathlib import Path
3+
4+
app_dir = Path(__file__).parent
5+
env_file = app_dir / ".env"
6+
7+
8+
def load_dotenv(dotenv_path: os.PathLike[str] = env_file, **kwargs) -> None:
9+
"""
10+
A convenience wrapper around `dotenv.load_dotenv` that warns if `dotenv` is not installed.
11+
It also returns `None` to make it easier to ignore the return value.
12+
"""
13+
try:
14+
import dotenv
15+
16+
dotenv.load_dotenv(dotenv_path=dotenv_path, **kwargs)
17+
except ImportError:
18+
import warnings
19+
20+
warnings.warn(
21+
"Could not import `dotenv`. If you want to use `.env` files to "
22+
"load environment variables, please install it using "
23+
"`pip install python-dotenv`.",
24+
stacklevel=2,
25+
)
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
shiny
2+
python-dotenv
3+
tokenizers
4+
openai

examples/chat/hello-providers/anthropic/app.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,16 @@
44
# To get one, follow the instructions at https://docs.anthropic.com/en/api/getting-started
55
# ------------------------------------------------------------------------------------
66
import os
7-
from pathlib import Path
87

98
from anthropic import AsyncAnthropic
10-
from dotenv import load_dotenv
9+
from app_utils import load_dotenv
1110

1211
from shiny.express import ui
1312

1413
# Either explicitly set the ANTHROPIC_API_KEY environment variable before launching the
1514
# app, or set them in a file named `.env`. The `python-dotenv` package will load `.env`
1615
# as environment variables which can later be read by `os.getenv()`.
17-
_ = load_dotenv(Path(__file__).parent / ".env")
18-
16+
load_dotenv()
1917
llm = AsyncAnthropic(api_key=os.environ.get("ANTHROPIC_API_KEY"))
2018

2119
# Set some Shiny page options
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import os
2+
from pathlib import Path
3+
4+
app_dir = Path(__file__).parent
5+
env_file = app_dir / ".env"
6+
7+
8+
def load_dotenv(dotenv_path: os.PathLike[str] = env_file, **kwargs) -> None:
9+
"""
10+
A convenience wrapper around `dotenv.load_dotenv` that warns if `dotenv` is not installed.
11+
It also returns `None` to make it easier to ignore the return value.
12+
"""
13+
try:
14+
import dotenv
15+
16+
dotenv.load_dotenv(dotenv_path=dotenv_path, **kwargs)
17+
except ImportError:
18+
import warnings
19+
20+
warnings.warn(
21+
"Could not import `dotenv`. If you want to use `.env` files to "
22+
"load environment variables, please install it using "
23+
"`pip install python-dotenv`.",
24+
stacklevel=2,
25+
)
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
shiny
2+
python-dotenv
3+
tokenizers
4+
anthropic

examples/chat/hello-providers/gemini/app.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,15 @@
33
# To run it, you'll need a Google API key.
44
# To get one, follow the instructions at https://ai.google.dev/gemini-api/docs/get-started/tutorial?lang=python
55
# ------------------------------------------------------------------------------------
6-
7-
from pathlib import Path
8-
9-
from dotenv import load_dotenv
6+
from app_utils import load_dotenv
107
from google.generativeai import GenerativeModel
118

129
from shiny.express import ui
1310

1411
# Either explicitly set the GOOGLE_API_KEY environment variable before launching the
1512
# app, or set them in a file named `.env`. The `python-dotenv` package will load `.env`
1613
# as environment variables which can later be read by `os.getenv()`.
17-
_ = load_dotenv(Path(__file__).parent / ".env")
18-
14+
load_dotenv()
1915
llm = GenerativeModel()
2016

2117
# Set some Shiny page options

0 commit comments

Comments
 (0)