# Streamlit Best Practices

Streamlit is an excellent tool for rapidly prototyping healthcare and data-driven applications. It automatically generates a user-friendly interface from your Python code, allowing you to focus on functionality and logic without needing to manually design front-end elements. This makes Streamlit a great starting point when developing apps for Health Universe.

Many developers choose to **start with Streamlit** when building a new app because:

* It allows for fast iteration: You can immediately visualize how inputs, outputs, and layout affect the user experience.
* You can debug and refine your logic more easily in a visual context.
* It supports integration with healthcare data formats like FHIR, CCDA, and CSV using native Python libraries.

Once the app’s behavior and interface are solid, developers then are able to easily migrate the codebase to FastAPI for production use with **Navigator**, where performance, modularity, and robust API control are priorities.

Here are some **best practices** to keep in mind when using Streamlit for your initial app development:

**1. Organize Your Code Clearly**

* Use functions or modular components to keep the code clean and readable.
* Separate function logic from UI logic where possible.

**2. Minimize Reruns**

* Use Streamlit’s `@st.cache_data`, `@st.cache_resource`, or `st.session_state` to store data or avoid expensive re-computations on every input change.
* Learn more: [Streamlit Caching Guide](https://docs.streamlit.io/1.27.2/library/advanced-features/caching)

**3. Use Layout Features Thoughtfully**

* Leverage columns (`st.columns()`), expanders (`st.expander()`), and sidebar components (`st.sidebar`) for better organization.
* Use `st.markdown` with custom HTML/CSS for layout tweaks when needed.

**4. Make It User-Friendly**

* Add clear labels, tooltips (`help=...`), and default values.
* Handle errors gracefully with `st.error()` or `try/except` blocks around sensitive logic.

**5. Test with Real Data**

* Test with a variety of realistic files (e.g., FHIR JSON, CCDAs) early on to uncover edge cases and formatting issues.

#### Helpful Links

* 📚 [Streamlit Documentation](https://docs.streamlit.io/)
* 🧪 [Cheat Sheet for Streamlit](https://docs.streamlit.io/library/cheatsheet)
* 💬 [Streamlit Community Forum](https://discuss.streamlit.io/)
* 🎨 [Streamlit Layout & Theming Guide](https://docs.streamlit.io/library/advanced-features/layouts)


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.healthuniverse.com/overview/building-apps-in-health-universe/developing-your-health-universe-app/working-in-streamlit/streamlit-best-practices.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
