How to add componment "dynamiclly"?

How to add componment dynamiclly?

I’m building a conversational app. In some cases, I use chatgpt to generate yaml file. I want to turn yaml into a form that makes it easier for users to modify the value, and then generate streamlit code and run it. The case show below.

My prompt and raw yaml

You are a assiant to help me convert CRD to python Streamlit code.

This is my redis cluster CRD.
```yaml
spec:
  kubernetesConfig:
    image: quay.io/opstree/redis:v6.2
    imagePullPolicy: IfNotPresent
    resources:
      requests:
        cpu: 101m
        memory: 128Mi
      limits:
        cpu: 101m
        memory: 128Mi
    redisSecret:
      name: redis-secret
      key: password
    imagePullSecrets:
      - name: regcred
```

Streamlit exmaple:

Number Input
```
import streamlit as st

number = st.number_input('Insert a number')
st.write('The current number is ', number)
```
Text Input
```
title = st.text_input('Movie title', 'Life of Brian')
st.write('The current movie title is', title)
```

Convert example:
Input: cpu: 101m
```
cpu = st.text_input('cpu', '101m')
```

Show me the python code answer

The code generated

import streamlit as st

# Input for CPU
cpu = st.text_input('CPU', '101m')
st.write('The current CPU value is', cpu)

# Input for Memory
memory = st.text_input('Memory', '128Mi')
st.write('The current Memory value is', memory)

# Input for Redis Secret Name
redis_secret_name = st.text_input('Redis Secret Name', 'redis-secret-name')
st.write('The current Redis Secret Name is', redis_secret_name)

# Input for Redis Secret Key
redis_secret_key = st.text_input('Redis Secret Key', 'password')
st.write('The current Redis Secret Key is', redis_secret_key)

# Input for Image
image = st.text_input('Image', 'quay.io/opstree/redis:v6.2')
st.write('The current image is', image)

# Input for Image Pull Policy

I want to embed this code dynamically in the dialog box without eval() method. Do you have any suggestions?

Hi @Rain_FD

Perhaps you can try using the Stlite Sandbox from the streamlit-extras component (πŸ“¦ Stlite Sandbox - streamlit-extras) to run your dynamically generated code.

import streamlit as st
from streamlit_extras.sandbox import sandbox

def dynamic_generated_code():
    import streamlit as st
    
    # Input for CPU
    cpu = st.text_input('CPU', '101m')
    st.write('The current CPU value is', cpu)
    
    # Input for Memory
    memory = st.text_input('Memory', '128Mi')
    st.write('The current Memory value is', memory)
    
    # Input for Redis Secret Name
    redis_secret_name = st.text_input('Redis Secret Name', 'redis-secret-name')
    st.write('The current Redis Secret Name is', redis_secret_name)
    
    # Input for Redis Secret Key
    redis_secret_key = st.text_input('Redis Secret Key', 'password')
    st.write('The current Redis Secret Key is', redis_secret_key)
    
    # Input for Image
    image = st.text_input('Image', 'quay.io/opstree/redis:v6.2')
    st.write('The current image is', image)

sandbox(dynamic_generated_code)

Hope this helps!

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.