CNI Research Note SEBI Reg. RA: INH000019512

CNI Finsight Institutional Research Archive

Proprietary equity analysis, valuation models & macro commentary by CNI InfoXchange Research Desk.

Equity Analysis Institutional Insights Market Trends 4 Min Read

guide karo : असली, genuine-Questions जो मैं Friend से पूछना चाहूँगा 1. Testing-Strategy बिना Live-Streamlit-Access के: मेरे पास genuinely-Live-Streamlit-App चलाने की Ability नहीं है (Sandbox-Restricted)। Point-7 में मैंने Byte-Identical Value-Tests किए थ

CNI InfoXchange Research Desk
INH000019512 · Published 12 Sep 2026 (3 months ago)
Share
Executive Research Takeaway

1. Testing‑strategy बिना live Streamlit access - Target रखो: “behavior टेस्ट करना, न कि pixels।” तुम्हें बस ये verify करना है कि सही widgets सही data के साथ call हो रहे हैं।

  1. Testing‑strategy बिना live Streamlit access
  • Target रखो: “behavior टेस्ट करना, न कि pixels।” तुम्हें बस ये verify करना है कि सही widgets सही data के साथ call हो रहे हैं।
  • Practical patterns (browser के बिना):
  • Mock streamlit API
  • unittest.mock या pytest-mock से streamlit.markdown, streamlit.columns, streamlit.text_input वगैरह patch कर दो।
  • अपनी section‑function call करो, फिर assert करो:
  • st.markdown कितनी बार call हुई,
  • किस text से call हुई,
  • columns layout कैसा बना (जैसे st.columns(3) आदि)।
  • इससे तुम्हें “UI‑rendering code path” पर byte‑level नहीं, लेकिन call‑level assurance मिल जाती है।
  • Thin wrapper / adapter pattern
  • अपनी हर UI section को ऐसे design करो:

``python def render_section_1(ui, data): ui.markdown("Title") c1, c2 = ui.columns(2) ... ``

  • Production में ui = streamlit दोगे,
  • टेस्ट में FakeUI class दोगे जो calls record करे (list में append) — फिर snapshot या structured comparison कर सकते हो।
  • Snapshot‑style tests
  • FakeUI के recorded calls (जैसे list of dicts: {"fn": "markdown", "args": ["..."], "kwargs": {...}}) को JSON में dump कर के snapshot compare कर सकते हो (“golden file” pattern)।
  • Key idea: backend जैसा byte‑identical यहाँ मुश्किल है, लेकिन “same sequence of Streamlit API calls” deterministic रख कर उसे test कर सकते हो — बिना live app खोले।
  1. Variable‑passing strategy – safest pattern

Streamlit के rerun‑model में “clarity & purity” सबसे बड़ी safety है।

  • (a) Explicit individual parameters – pure logic के लिए यह सबसे safe
  • Pure calculation / transformation functions को हमेशा explicit parameters दो, और pure return values लो।
  • इससे:
  • Rerun से कोई hidden side‑effect नहीं,
  • Debugging बहुत आसान।
  • (b) Shared context dict/object – UI orchestration के लिए controlled use OK
  • जब 12 sections में बहुत cross‑boundary variables हों, तब हर function signature 10–15 arguments से भर सकता है।
  • ऐसे में एक explicitly‑passed context (dict या dataclass) अच्छा compromise है:

```python @dataclass class PageContext: raw_data: pd.DataFrame pledge_pct: float ...

def section_1(ctx: PageContext): st.markdown("...") ... ```

  • Important: ये context global नहीं, बल्कि main() में create होकर हर section को argument की तरह जाए।
  • (c) Class‑based encapsulation – Streamlit context में अक्सर overkill / trickier
  • अगर तुम OOP comfortable हो और instance को st.session_state में रख रहे हो, तब ठीक है, लेकिन:
  • हर rerun में constructor कब चल रहा है, ये ध्यान रखना पड़ेगा,
  • Wrong pattern: global singleton object जो हर rerun में re‑init हो जाए और implicit state रखे।
  • Recommendation (इस specific Streamlit context में):
  • Pure backend: हमेशा (a) – explicit parameters, pure returns।
  • UI sections: (a) + छोटी, well‑defined context object (b) को combine करो।
  • Full class‑based app‑controller (c) सिर्फ तब जब तुम सच‑में OOP style app बनाना चाहते हो और उसका lifecycle session_state से manage कर सकते हो।
  1. Streamlit‑specific gotchas (session_state, widget keys, @st.fragment)
  • session_state
  • Key नाम / presence को deterministic रखो:

``python if "pledge_pct" not in st.session_state: st.session_state.pledge_pct = default_value ``

  • Sub‑functions में move करना safe है, बशर्ते logic और key‑names same रहें।
  • Subtle issues:
  • एक ही key को अलग code‑paths से अलग types से set करना (कभी float, कभी str) – future reruns में UI टूट सकता है।
  • Widget keys (जैसे key="pledge_pct_input")
  • Gotcha #1: key identity + call order बचा कर रखना।
  • अगर refactor के बाद widget किसी if branch में चला गया जो कभी‑कभी skip हो जाती है, तो उस key का widget उस rerun में नहीं बनेगा → state weird लगेगा।
  • Gotcha #2: एक ही key को एक से ज़्यादा बार use मत करो (refactor में accidentally duplicate हो सकता है)।
  • Functions में move करना खुद में safe है – जब तक:
  • हर rerun में वही function call sequence रहे,
  • वही keys उसी logical जगह पर create हों।
  • @st.fragment (या experimental decorators)
  • Fragment internal state / rendering थोड़ा अलग manage कर सकता है।
  • Move करने से subtle changes हो सकते हैं:
  • Fragment अलग से re‑run हो सकता है,
  • कुछ चीज़ें (जैसे st.set_page_config) fragments के अंदर allowed नहीं होतीं।
  • Best practice:
  • Fragment boundaries साफ रखो (एक logical “sub‑page” या “card” प्रति fragment),
  • session_state access fragment के अंदर भी वैसा ही predictable रखो जैसा पहले था।
  1. Staging‑strategy – तुम्हारा approach ठीक है?
  • “सबसे self‑contained section से शुरू करना” इस तरह के refactor के लिए काफी safe और established strategy है:
  • Low coupling → कम chance कि गलती से बाकी 11 sections पर ripple पड़े,
  • Pattern stabilize होने के बाद बाकी sections mechanical बन जाते हैं।
  • Alternate orders (जो लोग adopt करते हैं):
  • Bottom / last section से शुरू करना, क्योंकि उस पर forward dependencies कम होती हैं।
  • या पहले वो section जो सबसे ज़्यादा calculation + UI mix करता है, ताकि architecture जल्दी साफ हो जाए।
  • इस specific UI‑context में “mathematically best order” कोई नहीं है; risk control आता है इन principles से:
  1. पहले वो sections refactor करो जो कम session_state और कम cross‑section variables use करते हैं।
  2. हर refactor के बाद quickly run + sanity‑check (even headless tests) करो।
  3. Pattern lock हो जाए तो high‑coupling sections (जहाँ inter‑section dependencies ज़्यादा हैं) बाद में लो।
  • तुम्हारा proposed “self‑contained से शुरू” approach इन principles से align करता है, so इसे change करने की ज़रूरत नहीं है।
  1. Streamlit rerun‑model का genuine risk vs function‑extraction
  • Streamlit हर interaction पर पूरी script rerun करता है, लेकिन:
  • Top‑level function definitions हर बार re‑executed होते हैं, पर ये सिर्फ “define symbol” level का काम है – practical performance cost negligible है।
  • Real risk तब आता है जब तुम heavy काम (I/O, DB queries, big model load) top‑level पर या function definition के side‑effects में कर रहे हो।
  • Function‑extraction से क्या‑क्या ध्यान रखना है:
  • Avoid heavy work at import time:python

BAD

big_df = pd.read_csv("huge.csv") # हर rerun पर

BETTER

@st.cache_data def load_big_df(): return pd.read_csv("huge.csv") ``` और फिर इसको functions के अंदर call करो।

  • Pure UI functions cheap हैं:
  • def section_1(...): ... जैसा extraction fully compatible है rerun‑model से; यह सिर्फ code organize करता है, rerun behavior नहीं बदलता।
  • Mutable globals avoid करो:
  • अगर कोई global list/dict को functions mutate करते हैं, rerun‑model के साथ बहुत confusing हो जाएगा (क्योंकि हर rerun में वो फिर से init भी हो सकती है, या नहीं।)
  • State सिर्फ दो जगह: st.session_state या function‑local variables (जो हर rerun में नया बनता है)।
  • Net‑net:
  • Function‑extraction खुद rerun‑model के साथ conflict नहीं करता,
  • Risk सिर्फ वहीं है जहाँ heavy work या implicit mutable global state है।
  • Code को “pure + cached + explicit state” pattern में रखोगे, तो rerun + refactor दोनों काफी predictable रहेंगे।

If you have any further queries, please connect with us on 022-6290-10141 (Timings : 09.00 AM to 05.00 PM) or you can email us on info@cniinfoxchange.com

Discussion Thread (1 Consultations)

Follow-up Questions & Contextual Consultations

+ Start New Chat / Ask Another Stock
Ask a Follow-up Question on this Thread: The AI remembers the entire discussion above

Need Deeper Clarity on this Research?

Connect with our senior research analysts (Mon-Fri 09:00 AM - 05:00 PM IST)

Call 02269010141
SEBI Statutory Research Analyst Disclosure

CNI INFOXCHANGE PRIVATE LIMITED is a SEBI registered Research Analyst (Registration No. INH000019512, valid till January 2030). This research analysis and market insight is compiled for educational and informational purposes only and does not constitute personalized investment advice or an offer to buy/sell securities. Securities markets are subject to market risks. Read all scheme and offer related documents carefully before investing.