-
Notifications
You must be signed in to change notification settings - Fork 147
Add Streamlit support to Python Extensions (Experimental) #954
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 5 commits
b5de556
061bdaa
282844c
d19ecb3
31c6448
0b68f84
4b680f7
a739f4e
0e4a211
5425af5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| .venv/ | ||
| __pycache__/ | ||
| *.pyc |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| 3.12 |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| # Streamlit API Example | ||
|
|
||
| This is a simple Streamlit application that demonstrates how to run Streamlit apps in Aspire using the Python Extensions. | ||
|
|
||
| ## Prerequisites | ||
|
|
||
| - Python 3.12 or later | ||
| - .NET 8.0 or later | ||
| - Streamlit package | ||
|
|
||
| ## Setup | ||
|
|
||
| 1. Create a virtual environment: | ||
| ```bash | ||
| python -m venv .venv | ||
| ``` | ||
|
|
||
| 2. Activate the virtual environment: | ||
| - On Windows: | ||
| ```bash | ||
| .venv\Scripts\activate | ||
| ``` | ||
| - On macOS/Linux: | ||
| ```bash | ||
| source .venv/bin/activate | ||
| ``` | ||
|
|
||
| 3. Install dependencies: | ||
| ```bash | ||
| pip install streamlit | ||
| ``` | ||
|
|
||
| ## Running the App | ||
|
|
||
| The app can be run as part of the Aspire AppHost project or standalone. | ||
|
|
||
| ### Run with Aspire | ||
|
|
||
| Navigate to the AppHost project and run: | ||
| ```bash | ||
| dotnet run | ||
| ``` | ||
|
|
||
| ### Run Standalone | ||
|
|
||
| From this directory, run: | ||
| ```bash | ||
| streamlit run app.py | ||
| ``` | ||
|
|
||
| ## About the App | ||
|
|
||
| This is a basic Streamlit app that demonstrates: | ||
| - Simple text display | ||
| - Environment variable reading (PORT) | ||
| - Session state management | ||
| - Interactive buttons |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| import streamlit as st | ||
| import os | ||
|
|
||
| st.title("Hello, Aspire!") | ||
|
|
||
| st.write("This is a simple Streamlit app running in Aspire.") | ||
|
|
||
| port = os.environ.get("PORT", "8501") | ||
| st.write(f"Running on port: {port}") | ||
|
|
||
| # Add a simple counter | ||
| if "counter" not in st.session_state: | ||
| st.session_state.counter = 0 | ||
|
|
||
| if st.button("Click me!"): | ||
| st.session_state.counter += 1 | ||
|
|
||
| st.write(f"Button clicked {st.session_state.counter} times") |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| [project] | ||
| name = "streamlit-api" | ||
| version = "0.1.0" | ||
| description = "Test project for streamlit-api" | ||
| requires-python = ">=3.12" | ||
| dependencies = [ | ||
| "streamlit>=1.40.0", | ||
| ] | ||
|
|
||
| [build-system] | ||
| requires = ["hatchling"] | ||
| build-backend = "hatchling.build" |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| using Aspire.Hosting.ApplicationModel; | ||
| using CommunityToolkit.Aspire.Utils; | ||
|
|
||
| namespace Aspire.Hosting; | ||
|
|
||
| /// <summary> | ||
| /// Provides extension methods for adding Streamlit applications to an <see cref="IDistributedApplicationBuilder"/>. | ||
| /// </summary> | ||
| public static class StreamlitAppHostingExtension | ||
| { | ||
| /// <summary> | ||
| /// Adds a Streamlit application to the distributed application builder. | ||
| /// </summary> | ||
| /// <param name="builder">The distributed application builder.</param> | ||
| /// <param name="name">The name of the Streamlit application.</param> | ||
| /// <param name="projectDirectory">The directory of the project containing the Streamlit application.</param> | ||
| /// <param name="scriptPath">The path to the Python script to be run by Streamlit.</param> | ||
| /// <param name="args">Optional arguments to pass to the Streamlit command.</param> | ||
| /// <returns>An <see cref="IResourceBuilder{StreamlitAppResource}"/> for the Streamlit application resource.</returns> | ||
| /// <exception cref="ArgumentNullException">Thrown if <paramref name="builder"/> is null.</exception> | ||
| /// <exception cref="ArgumentException">Thrown if <paramref name="name"/> or <paramref name="scriptPath"/> is null or empty.</exception> | ||
| public static IResourceBuilder<StreamlitAppResource> AddStreamlitApp( | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does debugging and deploying work?
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wouldn't invest in that until 13.1 when we can drop our implementation of the internals of the Python integration.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we should make sure it works. The bar should be higher in 13 for new language / framework integrations. Even if it doesn't work I don't want us to merge things without understanding what works and what doesn't work. I'd expect the future to look like a set of capabililities for these language integrations (otel support, endpoints, https, dockerfile, debugging). Doing enough work to know what we will document is a minimum bar here.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. While I appreciate the desire, we have to work with the realities of the OSS project - we don't have the resources to do everything that Aspire does. The goal is to do our best to be producing high quality integrations to the level of Aspire, but it's not always going to be the case, especially if we want to ship. Feel free to raise an issue to get debugger support enabled, but I don't see it as a blocker for us shipping.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok let me ask then. Does it work when you run locally and deploy? If it doesn’t? Do we know why? Has anyone built an app with this PR? |
||
| this IDistributedApplicationBuilder builder, | ||
| [ResourceName] string name, | ||
| string projectDirectory, | ||
| string scriptPath, | ||
| params string[] args) | ||
| { | ||
| return builder.AddStreamlitApp(name, projectDirectory, scriptPath, ".venv", args); | ||
| } | ||
|
|
||
| private static IResourceBuilder<StreamlitAppResource> AddStreamlitApp( | ||
| this IDistributedApplicationBuilder builder, | ||
| string name, | ||
| string projectDirectory, | ||
| string scriptPath, | ||
| string virtualEnvironmentPath, | ||
| params string[] args) | ||
tommasodotNET marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| { | ||
| ArgumentNullException.ThrowIfNull(builder); | ||
| ArgumentException.ThrowIfNullOrWhiteSpace(name); | ||
| ArgumentException.ThrowIfNullOrWhiteSpace(scriptPath); | ||
|
|
||
| string wd = projectDirectory ?? Path.Combine("..", name); | ||
tommasodotNET marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| projectDirectory = PathNormalizer.NormalizePathForCurrentPlatform(Path.Combine(builder.AppHostDirectory, wd)); | ||
tommasodotNET marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| var projectResource = new StreamlitAppResource(name, projectDirectory); | ||
|
|
||
aaronpowell marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| return builder.AddResource(projectResource) | ||
| .WithEnvironment(context => | ||
| { | ||
| // Streamlit uses STREAMLIT_SERVER_PORT instead of PORT, so map PORT to STREAMLIT_SERVER_PORT | ||
| if (context.EnvironmentVariables.TryGetValue("PORT", out var portValue)) | ||
| { | ||
| context.EnvironmentVariables["STREAMLIT_SERVER_PORT"] = portValue; | ||
| } | ||
| }) | ||
| .WithArgs(context => | ||
| { | ||
| AddProjectArguments(scriptPath, args, context); | ||
| }); | ||
| } | ||
|
|
||
| private static void AddProjectArguments(string scriptPath, string[] scriptArgs, CommandLineArgsCallbackContext context) | ||
| { | ||
| context.Args.Add("run"); | ||
| context.Args.Add(scriptPath); | ||
|
|
||
| // Add --server.headless to run without browser opening | ||
| context.Args.Add("--server.headless"); | ||
| context.Args.Add("true"); | ||
|
|
||
| foreach (var arg in scriptArgs) | ||
| { | ||
| context.Args.Add(arg); | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| using Aspire.Hosting.Python; | ||
|
|
||
| namespace Aspire.Hosting.ApplicationModel; | ||
|
|
||
| /// <summary> | ||
| /// Represents a Streamlit application. | ||
| /// </summary> | ||
| /// <param name="name">The name of the resource.</param> | ||
| /// <param name="workingDirectory">The working directory for streamlit.</param> | ||
| public class StreamlitAppResource(string name, string workingDirectory) | ||
| : PythonAppResource(name, "streamlit", workingDirectory), IResourceWithServiceDiscovery; |
Uh oh!
There was an error while loading. Please reload this page.