Repository: truera/trulens
Issue: Deploy Trulens with FastAPI
Root cause: Documentation Deficiency

1) What is the approach to deploy trulens with FastAPI ?
2) How to run tru.run_dashboard() in a single container with FastAPI with Uvicorn Server ?

<!-- Greeting -->
Hey @Nishant01-ai! I'm here to help you with any bugs, questions, or contributions while you're waiting for a human maintainer. Let's tackle this together!

<!-- Answer -->
To deploy Trulens with FastAPI and run `tru.run_dashboard()` in a single container using a Uvicorn server, follow these steps:

1. **Create a FastAPI application.**
2. **Integrate the Trulens dashboard.**
3. **Use Uvicorn to serve the application.**

Here is a sample code to demonstrate this:

```python
from fastapi import FastAPI
import uvicorn
from trulens_eval import Tru

app = FastAPI()

@app.on_event("startup")
async def startup_event():
    tru = Tru()
    tru.run_dashboard(port=8501)  # Specify the port for the dashboard

@app.get("/")
async def read_root():
    return {"Hello": "World"}

if __name__ == "__main__":
    uvicorn.run(app, host="0.0.0.0", port=8000)
```

In this code:
- A FastAPI application instance is created.
- A startup event is defined to run the Trulens dashboard when the application starts.
- The port for the Trulens dashboard is specified using the `port` parameter in `tru.run_dashboard()`.
- A simple root endpoint is defined for demonstration.
- Uvicorn is used to run the FastAPI application.

This setup will allow you to run both the FastAPI application and the Trulens dashboard in a single container <sup>[[1]](https://github.com/truera/trulens/blob/main/trulens_eval/trulens_eval/utils/command_line.py)</sup>.

**Note:** There are known issues or limitations when running the Trulens dashboard and FastAPI in the same container. The Trulens dashboard uses Streamlit, which runs its own server. This can lead to conflicts if both the FastAPI application and the Streamlit dashboard are trying to bind to the same port or if there are resource constraints within the container <sup>[[2]](https://github.com/truera/trulens/blob/main/trulens_eval/trulens_eval/tru.py)</sup>.


<!-- Dosu Comment Footer -->
<details open>
<summary>

#### Details
</summary>

**About Dosu**
This response is meant to be useful and save you time. It is **not meant to be a precise solution**, but rather a starting point for your own research.

Help me be more useful! Please leave a 👍 if this is helpful and 👎 if it is irrelevant.
</details>

*To continue the conversation, mention @dosu.*


@dosu response is good here. Let us know if you have issues

<!-- Answer -->
To deploy Trulens with FastAPI and run `tru.run_dashboard()` in a single container using a Uvicorn server, follow these steps:

1. **Create a FastAPI application.**
2. **Integrate the Trulens dashboard.**
3. **Use Uvicorn to serve the application.**

Here is a sample code to demonstrate this:

```python
from fastapi import FastAPI
import uvicorn
from trulens_eval import Tru

app = FastAPI()

@app.on_event("startup")
async def startup_event():
    tru = Tru()
    tru.run_dashboard(port=8501)  # Specify the port for the dashboard

@app.get("/")
async def read_root():
    return {"Hello": "World"}

if __name__ == "__main__":
    uvicorn.run(app, host="0.0.0.0", port=8000)
```

In this code:
- A FastAPI application instance is created.
- A startup event is defined to run the Trulens dashboard when the application starts.
- The port for the Trulens dashboard is specified using the `port` parameter in `tru.run_dashboard()`.
- A simple root endpoint is defined for demonstration.
- Uvicorn is used to run the FastAPI application.

This setup will allow you to run both the FastAPI application and the Trulens dashboard in a single container .

**Note:** There are known issues or limitations when running the Trulens dashboard and FastAPI in the same container. The Trulens dashboard uses Streamlit, which runs its own server. This can lead to conflicts if both the FastAPI application and the Streamlit dashboard are trying to bind to the same port or if there are resource constraints within the container .


<!-- Dosu Comment Footer -->
*To continue talking to [Dosu](https://dosu.dev), mention @dosu.*


No further work planned on this.

@sfc-gh-chu also had some issues with working with an app defined by fastapi endpoints.