Skip to content

Commit 833f255

Browse files
feat(example): streamlit app
1 parent cb3dc18 commit 833f255

5 files changed

Lines changed: 523 additions & 14 deletions

File tree

CONTRIBUTING.md

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,6 @@ uv run pre-commit install # install pre-commit hooks, see https://pr
2525

2626
### Linux
2727

28-
Notes:
29-
30-
- Not yet validated
31-
- .github/workflows/test-and-report.yml might provide further information
32-
3328
```shell
3429
sudo sudo apt install -y curl jq libxml2-utils gnupg2 # tooling
3530
curl --proto '=https' --tlsv1.2 -sSf https://raw.githubusercontent.com/nektos/act/master/install.sh | sudo bash # act
@@ -46,11 +41,28 @@ src/brave_search_python_client/
4641
├── constants.py # Constants
4742
├── requests.py # Pydantic models for requests
4843
└── responses/ # Pydantic models for responses
49-
tests/
44+
tests/ # Unit and E2E tests
5045
├── client_test.py # Client tests including response validation
5146
├── requests_tests.py # Tests for request validation
5247
├── cli_test.py # CLI tests
5348
└── fixtures/ # Example responses
49+
examples/ # Example code demonstrating use of hte client
50+
├── streamlit.py # Streamlit App
51+
```
52+
53+
## Run
54+
55+
### CLI
56+
57+
```bash
58+
uv run brave-search-python-client --help
59+
```
60+
61+
### Extra: Streamlit Example App
62+
63+
```bash
64+
uv sync --all-extras # streamlit is an extra of this package
65+
sreamlit run examples/streamlit.py
5466
```
5567

5668
## Build
@@ -69,18 +81,18 @@ uv run nox -s lint # run formatting and linting
6981
uv run nox -s audit # run security and license audit, inc. sbom generation
7082
```
7183

72-
## Running GitHub CI workflow locally
73-
74-
Notes:
75-
76-
- Workflow defined in .github/workflows/ci.yml
77-
- Calls all build steps defined in noxfile.py
84+
### Running GitHub CI workflow locally
7885

7986
```shell
8087
./github-action-run.sh
8188
```
8289

83-
## Docker
90+
Notes:
91+
92+
- Workflow defined in .github/workflows/*.yml
93+
- test-and-report.yml calls all build steps defined in noxfile.py
94+
95+
### Docker
8496

8597
```shell
8698
docker build -t brave-search-python-client .

examples/streamlit.py

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import asyncio
2+
3+
import streamlit as st
4+
5+
from brave_search_python_client import (
6+
BraveSearch,
7+
WebSearchApiResponse,
8+
WebSearchRequest,
9+
__version__,
10+
)
11+
12+
sidebar = st.sidebar
13+
api_key = api_key = sidebar.text_input(
14+
"[Brave Search API key](https://brave.com/search/api/)"
15+
)
16+
sidebar.write(
17+
f" [Brave Search Python Client v{__version__}](https://helmuthva.gitbook.io/brave-search-python-client)"
18+
)
19+
sidebar.write("Built with love in Berlin 🐻")
20+
21+
st.title("🦁 Brave Search ")
22+
q = st.text_input("Query")
23+
24+
if api_key and q:
25+
# Initialize the BraveSearch client
26+
bs = BraveSearch(api_key=api_key)
27+
28+
@st.cache_data
29+
def search(q: str) -> WebSearchApiResponse:
30+
"""
31+
Performs a synchronous web search using Brave Search API.
32+
33+
This function wraps an asynchronous API call into a synchronous interface by using
34+
asyncio.run(). It executes a web search query through the Brave Search API.
35+
36+
Args:
37+
q (str): The search query string to be sent to Brave Search
38+
39+
Returns:
40+
WebSearchApiResponse: The response object from Brave Search API containing search results
41+
42+
Example:
43+
>>> response = search("python programming")
44+
>>> print(response.web.results[0].title)
45+
"""
46+
return asyncio.run(bs.web(WebSearchRequest(q=q)))
47+
48+
# Perform the search
49+
response = search(q)
50+
51+
# Print the response as JSON
52+
cols = st.columns([0.1, 0.9])
53+
with cols[0]:
54+
st.write("JSON:")
55+
with cols[1]:
56+
st.json(response.model_dump(), expanded=False)
57+
58+
# Print the search results of type web
59+
for result in response.web.results if response.web else []:
60+
st.write(f"[{result.title}]({result.url})")
61+
elif q:
62+
st.write(
63+
"Please enter your [Brave Search API key](https://brave.com/search/api/) in the sidebar."
64+
)

pyproject.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
name = "brave-search-python-client"
33
version = "0.0.6"
44
description = "🦁 Brave Search Python Client supporting Web, Image, News and Video search."
5+
readme = "README.md"
56
authors = [
67
{ name = "Helmut Hoffer von Ankershoffen", email = "helmuthva@googlemail.com" },
78
]
@@ -23,6 +24,7 @@ keywords = [
2324
"typer",
2425
"uv",
2526
]
27+
2628
classifiers = [
2729
"Development Status :: 2 - Pre-Alpha",
2830
"Intended Audience :: Developers",
@@ -90,6 +92,8 @@ dev = [
9092

9193

9294
[project.optional-dependencies]
95+
streamlit = ["streamlit>=1.41.1"]
96+
9397
# used by uv pip install -e ".[dev]", as used within nox (uv run nox)
9498
dev = [
9599
"bump-my-version>=0.29.0",

sonar-project.properties

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,5 @@ sonar.links.ci=https://github.com/helmut-hoffer-von-ankershoffen/brave-search-py
88
sonar.links.issues=https://github.com/helmut-hoffer-von-ankershoffen/brave-search-python-client/issues
99
sonar.python.coverage.reportPaths=coverage.xml
1010
sonar.python.version=3.11, 3.12, 3.13
11-
sonar.coverage.exclusions=noxfile.py, tests/**
11+
sonar.coverage.exclusions=noxfile.py, tests/**, examples/**
12+
sonar.exclusions=examples/**

0 commit comments

Comments
 (0)