barfi icon indicating copy to clipboard operation
barfi copied to clipboard

set_option problem

Open erwanito12 opened this issue 2 years ago • 9 comments

hello, with this script I am trying to display the output of the slider block in a result block without result. what is wrong with my script? thanks in advance

from barfi import st_barfi, barfi_schemas, Block import streamlit as st

slider_block = Block(name='Slider')

slider_block.add_input() slider_block.add_output() slider_block.add_option(name='display-option', type='display',value='This is a Block with Slider option.') slider_block.add_option(name='slider-option-1', type='slider', min=0, max=10, value=2.5)

def slider_block_func(self):
input_1_value = self.get_interface(name='Input 1')

slider_1_value = self.get_option(name='slider-option-1')

self.set_interface(name='Output 1', value=slider_1_value)

slider_block.add_compute(slider_block_func)

result = Block(name='Result') result.add_input() result.add_option(name='ValueText', type='display', value='toto')

def result_func(self): in_1 = self.get_interface(name='Input 1')
self.set_option(name='ValueText',value=str(in_1)) result.add_compute(result_func)

compute_engine = st.checkbox('Activate barfi compute engine', value=True) barfi_result = st_barfi(base_blocks=[ slider_block, result]) if barfi_result: st.write(barfi_result)

erwanito12 avatar Aug 30 '22 12:08 erwanito12

I want to reproduce this example:

export const DisplayNode = new NodeBuilder("DisplayNode") .setName("Display") .addInputInterface("Value") .addOption("ValueText", "TextOption")

.onCalculate(n => {
    let value = n.getInterface("Value").value;

    n.setOptionValue("ValueText", value);
})
.build();

import { Node } from "@baklavajs/core";

export class MathNode extends Node { constructor() { super(); this.type = "MathNode"; this.name = "Math"; this.addInputInterface("Number 1", "SliderOption", 100);

    this.addOutputInterface("Result");
}

calculate() {
    const n1 = this.getInterface("Number 1").value;
   
    this.getInterface("Result").value = n1;
}

}

erwanito12 avatar Aug 30 '22 13:08 erwanito12

Hello @erwanito12

If I understood you right, you want to display the value from the slider on the result block, updating the string toto to that value.

Then, that is not possible at the moment. (Nor do I see that feature in the roadmap.) The display option is on the initial block creation. And cannot be updated later on schema execution.

But you can get the result from the result block and display it on the app, by using:

if barfi_result:
    st.write(barfi_result['Result-1']['block'].get_interface('Input 1'))

Screenshot 2022-08-30 at 23 32 25

krish-adi avatar Aug 30 '22 21:08 krish-adi

Thank you @krish-adi for your detailed answer, you understood my request very well. my more general idea is to be able to add options to your library. I achieved this by modifying the files in the frontEnd folder and at option_builder.py. I want to be able to modify a canvas like I do with baklavajs. indeed the value cannot be updated within the framework of your library, it's a real shame that it greatly limits the use and ergonomics of your library. congratulations for your excellent work.

image

erwanito12 avatar Aug 30 '22 23:08 erwanito12

which gives as result:

image

erwanito12 avatar Aug 30 '22 23:08 erwanito12

with this script:

from barfi import st_barfi, barfi_schemas, Block import streamlit as st

feed = Block(name='Feed') feed.add_input() feed.add_option(name = 'Peak', type = 'PeakOption', value=0.4)

def feed_func(self):

input_1_value = self.get_interface(name='Input 1')
self.set_option(name='Peak', value=input_1_value)

feed.add_compute(feed_func)

slider_block = Block(name='Slider')

slider_block.add_input() slider_block.add_output()

slider_block.add_option(name='display-option', type='display',value='This is a Block with Slider option.') slider_block.add_option(name='slider-option-1', type='slider', min=0, max=10, value=2.5)

def slider_block_func(self): input_1_value = self.get_interface(name='Input 1') slider_1_value = self.get_option(name='slider-option-1')
self.set_interface(name='Output 1', value=slider_1_value) slider_block.add_compute(slider_block_func)

result = Block(name='Result') result.add_input() result.add_option(name='ValueText', type='display')

def result_func(self): in_1 = self.get_interface(name='Input 1')
self.set_option(name='ValueText',value=str(in_1)) result.add_compute(result_func)

result.set_option(name='ValueText',value='toto') compute_engine = st.checkbox('Activate barfi compute engine', value=True) barfi_result = st_barfi(base_blocks=[ feed, slider_block, result]) if barfi_result: st.write(barfi_result['Result-1']['block'].get_interface('Input 1'))

erwanito12 avatar Aug 30 '22 23:08 erwanito12

hey erwanito12 would you mind to share your scripts? I like to play with this too as we got the same requirements. We like to build a block which could show a result image or maybe more

osi1880vr avatar Sep 28 '22 05:09 osi1880vr

@erwanito12 Sorry, I missed this. Could you send a PR with an example to test it?

krish-adi avatar Sep 28 '22 09:09 krish-adi

can i add a dialog box popup to add a form in the block

restructai avatar Sep 09 '23 11:09 restructai

I'm also looking forward for a way on how to update (rerender) a Display in the canvas and add a feature for dobule click in a block and open a modal with maibe the str method of the block or a custom modal without rerun the streamlit.

Lovely and simple module/widget great work anyway

Sendery avatar Oct 20 '23 10:10 Sendery