Adapting DeepResearcher for Streamlit Integration

Introduction: Making DeepResearcher Work with Streamlit

Welcome back! In the last lesson, you built a basic Streamlit page for your DeepResearcher app. You added a title, a description, and some user input widgets. Now, you are ready to connect your Streamlit frontend to the backend research logic.

To do this, we need to adapt the DeepResearcher backend code so it works smoothly with a web interface. The original backend was designed to run in the terminal, asking for user input and printing results. For a web app, we want the backend to accept input as function arguments and return results, so Streamlit can display them.

In this lesson, you will learn how to refactor the main DeepResearcher module to make it ready for integration with your Streamlit app.

Recall: How the Old main.py Worked

Let’s quickly remind ourselves how the old main.py worked. Previously, the main function looked something like this:

def research_main():
    user_query = input("Enter your research query/topic: ").strip()
    iteration_limit = input("Max number of iterations (default 10): ").strip()
    iteration_limit = int(iteration_limit) if iteration_limit.isdigit() else 10
    # ... rest of the code ...
  • The function asked the user for a research topic and the number of iterations using input().
  • It then processed the research and printed the final report using print().

This approach works for command-line programs, but it does not fit well with a web app, where user input comes from the browser and results need to be returned to the frontend.

Refactoring Step 1: Accepting Arguments Instead of User Input

The first step is to change the research_main function so it takes arguments instead of asking for input. This makes it possible for Streamlit (or any other code) to call the function directly with the needed values.

Let’s look at how to do this:

Old code:

def research_main():
    user_query = input("Enter your research query/topic: ").strip()
    iteration_limit = input("Max number of iterations (default 10): ").strip()
    iteration_limit = int(iteration_limit) if iteration_limit.isdigit() else 10
    # ... rest of the code ...

New code:

def research_main(user_query, iteration_limit):
    # ... rest of the code ...
  • We removed the input() calls.
  • Now, user_query and iteration_limit are parameters to the function.
  • This means you can call research_main("What is quantum computing?", 5) directly from your code.

Why is this important?
By accepting arguments, the function becomes reusable and easier to test. It also allows the Streamlit frontend to pass user input directly to the backend function.

Sign up

Join the 1M+ learners on CodeSignal

Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal