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.
Let’s quickly remind ourselves how the old main.py
worked. Previously, the main function looked something like this:
- 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.
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:
New code:
- We removed the
input()
calls. - Now,
user_query
anditeration_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.
Next, we want the function to return the final report instead of printing it. This way, the Streamlit app can display the result in the browser.
Let’s see how to do this step by step.
Old code:
- The function prints the final report to the terminal.
New code:
- Now, the function returns
final_report
at the end. - The
print()
statements can stay for debugging, but returning the value is what allows the frontend to use the result.
Updating the main function:
- The main function now returns the final report, making it easy for Streamlit to display it.
Example usage:
Output:
In the original script, you might have seen this at the bottom:
This block makes the script run when you execute it directly from the command line. However, for a module that will be imported and used by another script (like your Streamlit app), this is not needed.
What should you do?
- Remove the
if __name__ == "__main__":
block. - This makes your code cleaner and easier to import as a module.
Why is this important?
Without this block, your functions are available to be called from anywhere, including your Streamlit frontend. This is a common practice when building code that will be reused in different places.
In this lesson, you learned how to adapt the DeepResearcher backend so it can work with your Streamlit frontend. Here’s what you did:
- Changed the main function to accept arguments instead of using
input()
. - Updated the code to return the final report instead of just printing it.
- Removed the script entry point to make the code easier to import and use.
These changes make your backend more flexible and ready for integration with your web app. In the next practice exercises, you’ll get hands-on experience refactoring the code yourself and see how these changes help you connect everything together.
