Skip to content

Commit 20cc992

Browse files
committed
multi page setup and single need viewer
1 parent 7d68b72 commit 20cc992

File tree

5 files changed

+101
-0
lines changed

5 files changed

+101
-0
lines changed

.envrc

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
source .venv/bin/activate
File renamed without changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import json
2+
import urllib.request
3+
4+
import streamlit as st
5+
from streamlit_agraph import agraph, Node, Edge, Config
6+
7+
from sphinx_needs_viewer.util import get_need, get_needs_ids
8+
9+
NEEDS_URL = "https://sphinx-needs.readthedocs.io/en/latest/needs.json"
10+
11+
@st.cache_data
12+
def get_needs_data(needs_url): # noqa: D103
13+
with urllib.request.urlopen(needs_url) as url: # noqa: S310
14+
return json.load(url)
15+
16+
# Page config
17+
st.set_page_config(
18+
page_title="Single Need Viewer",
19+
page_icon="📦",
20+
layout="wide",
21+
initial_sidebar_state="expanded",
22+
menu_items={
23+
"Report a bug": "https://github.com/useblocks/sphinx-needs-viewer",
24+
},
25+
)
26+
27+
needs_url = st.text_input("**needs.json URL**", NEEDS_URL)
28+
need_ids = get_needs_ids(needs_url)
29+
url_params = st.query_params
30+
selected_id_index = 0
31+
if "id" in url_params:
32+
selectd_id = url_params['id']
33+
try:
34+
selected_id_index = list(need_ids).index(selectd_id)
35+
except ValueError:
36+
selected_id_index = 0 # ID not found
37+
38+
need_id = option = st.selectbox(
39+
"Needs ID",need_ids, index=selected_id_index)
40+
need = get_need(needs_url, need_id)
41+
42+
43+
if not need:
44+
st.write(f"Need with ID {need_id} ot found")
45+
else:
46+
st.markdown(f"""
47+
## {need["id"]}: {need["title"]}
48+
status: **{need["status"]}**
49+
50+
doc: **{need["docname"]}{need["doctype"]}**
51+
52+
link: https://sphinx-needs.readthedocs.io/en/latest/{need["docname"]}.html#{need["id"]}
53+
54+
{need["description"]}
55+
""")
56+
57+
st.dataframe(need, use_container_width=True)

sphinx_needs_viewer/util.py

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import json
2+
import urllib.request
3+
import sys
4+
5+
import streamlit as st
6+
7+
@st.cache_data
8+
def get_needs_data(needs_url): # noqa: D103
9+
try:
10+
with urllib.request.urlopen(needs_url) as url: # noqa: S310
11+
return json.load(url)
12+
except Exception:
13+
sys.exit(0)
14+
15+
@st.cache_data
16+
def get_prepared_needs(needs_url, max_needs=20):
17+
needs_raw_data = get_needs_data(needs_url)
18+
needs_data = needs_raw_data["versions"]["2.1.0"]["needs"]
19+
needs_data_reduced = {x['id']:x for x in list(needs_raw_data["versions"]["2.1.0"]["needs"].values())[0:max_needs]}
20+
return needs_data_reduced
21+
22+
@st.cache_data
23+
def get_need(needs_url, need_id):
24+
needs_raw_data = get_needs_data(needs_url)
25+
needs_data = needs_raw_data["versions"]["2.1.0"]["needs"]
26+
try:
27+
return needs_data[need_id]
28+
except KeyError:
29+
return False
30+
31+
def get_needs_ids(needs_url):
32+
needs_raw_data = get_needs_data(needs_url)
33+
return needs_raw_data["versions"]["2.1.0"]["needs"].keys()

sphinx_needs_viewer/welcome.py

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import streamlit as st
2+
3+
st.set_page_config(
4+
page_title="Hello",
5+
page_icon="👋",
6+
)
7+
8+
st.write("# Welcome to Streamlit! 👋")
9+
10+
st.sidebar.success("Select a demo above.")

0 commit comments

Comments
 (0)