How and when to use databases and advanced skills
In Chapter 13, when we talked about login and signup, we explained how to deal with databases in Streamlit, while in Chapters 14 and 15, we covered topics such as customized pages, multi-pages, themes, forms, subdomains, and Session State.
Now, it may sound trivial, but you should consider using databases in Streamlit when you need to store and manage data for your web application. Databases are useful when you want to do the following:
- Persist data: Save information (such as user profiles, user-generated content, or application settings) so that it’s available even after closing the web app.
- Access large amounts of data: Handle and retrieve a lot of data efficiently, such as records in a sales database or user comments on a website.
- Collaborate: Share and update data across multiple users or devices. Databases help you keep information consistent and up to date.
When working with databases in Streamlit, or any web application, it’s important to follow the best security practices, which include the following:
- Use prepared statements: When writing SQL queries, use prepared statements or parameterized queries to prevent SQL injection attacks. Streamlit has a library called sqlite3 that allows you to work with databases.
- Secure database credentials: Store your database credentials (such as usernames and passwords) in a secure environment, such as environment variables or a configuration file. Never hardcode them in your code.
- Implement authentication: If your application allows users to access or modify data, implement user authentication and authorization to control who can do what in your app.
- Regularly update and patch: Keep your database software and libraries up to date to fix security vulnerabilities. This is important for both your application and the database system itself.
- Data encryption: If sensitive data is stored in the database, ensure it’s encrypted both at rest (when stored on the server) and in transit (when data is transmitted between your app and the database).
In simple terms, databases are like organized storage spaces for your app’s data. They’re useful for saving, accessing, and managing information securely. To use them safely in Streamlit, follow the best practices to protect your data and ensure your app runs smoothly. Avoid common mistakes such as not handling errors, exposing sensitive data, or overloading the database.
Here’s a brief recap of some advanced Streamlit features:
- Customized pages: Streamlit allows you to create customized pages within your web application. Each page can have unique content, layout, and functionality. This feature is beneficial when you want to organize your application into different sections, such as a homepage, a dashboard, and a settings page. You can use Streamlit’s layout components to design and structure each page as you like.
- Multiple pages: Streamlit offers the ability to create web applications with multiple interconnected pages. This allows you to build complex applications with different views, navigation, and interactions. You can use Streamlit’s built-in navigation elements, such as buttons or links, to move between pages within your app. Multi-page applications are useful for creating applications with a hierarchical structure or when you need to guide users through different steps.
- Themes: Streamlit provides the ability to customize the visual appearance of your application with themes. Themes are collections of predefined styles and colors that you can apply to your app. This feature lets you create a consistent and visually appealing design that matches your branding or personal preferences.
- Forms: Streamlit allows you to create interactive forms within your web application. Forms are a way to gather user input through widgets such as text inputs, sliders, and checkboxes. You can use forms to collect data, settings, or preferences from users and process that information in your application.
- Subdomains: Subdomains in Streamlit enable you to serve different applications or sections of your app from distinct subdomains of your website. This is especially useful when you want to host multiple Streamlit apps under different URLs, each with its own functionality or content. Subdomains help in creating a modular and organized structure for your web application, allowing for efficient navigation and scalability.
- Session State: Session State in Streamlit lets you store and manage data across different interactions and pages during a user’s session. It’s a way of maintaining variables or user-specific data that persists as the user interacts with your app. This feature is useful if you want the application to remember user settings, selections, or other information gained during users’ visits. This can provide a more personalized and dynamic user experience.
To recap, Streamlit offers a variety of features to enhance the functionality and appearance of your web applications. You can create customized pages for different sections, design multi-page applications with navigation, style your app with themes, collect user input using forms, use subdomains for modularity, and maintain session-specific data with Session State. These features provide you with the flexibility and tools needed to build interactive and user-friendly web applications using Streamlit.
After developing our web application, which uses a database to save information, it’s time to deploy it.