Spaces:
Running
Running
Upload 3 files
Browse files- app.py +8 -0
- pages/001 link demo.py +64 -0
- pages/002 Bokeh scatter bekoh plot.py +77 -0
app.py
ADDED
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import simplestart as ss
|
2 |
+
|
3 |
+
ss.md('''
|
4 |
+
# Introduction
|
5 |
+
|
6 |
+
Some demos of SimpleStart
|
7 |
+
|
8 |
+
''')
|
pages/001 link demo.py
ADDED
@@ -0,0 +1,64 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
### A link to download Qrcode
|
2 |
+
|
3 |
+
# this is demo is based on the topic discussed on stackoverflow
|
4 |
+
# https://stackoverflow.com/questions/78045476/streamlit-re-runs-app-on-button-click-delaying-download/79008691#79008691
|
5 |
+
|
6 |
+
import simplestart as ss
|
7 |
+
|
8 |
+
ss.md('''
|
9 |
+
#### In this demo, you can change the URL in the input to generate a QR code image. Click the link below to download it.
|
10 |
+
---
|
11 |
+
''')
|
12 |
+
|
13 |
+
import qrcode
|
14 |
+
from io import BytesIO
|
15 |
+
from PIL import Image
|
16 |
+
|
17 |
+
#api
|
18 |
+
def generate_qr_code(link): # Generate QR Code
|
19 |
+
qr = qrcode.QRCode(
|
20 |
+
version=1.00,
|
21 |
+
error_correction=qrcode.constants.ERROR_CORRECT_L,
|
22 |
+
box_size=10,
|
23 |
+
border=2,
|
24 |
+
)
|
25 |
+
qr.add_data(link)
|
26 |
+
qr.make(fit=True)
|
27 |
+
img = qr.make_image(fill_color="black", back_color="white")
|
28 |
+
return img
|
29 |
+
|
30 |
+
|
31 |
+
def download_qr_code(img): # Convert PIL image to bytes
|
32 |
+
img_bytes = BytesIO()
|
33 |
+
img.save(img_bytes, format='PNG')
|
34 |
+
img_bytes.seek(0)
|
35 |
+
return img_bytes
|
36 |
+
|
37 |
+
#event handler
|
38 |
+
def text_change(event):
|
39 |
+
generate_img()
|
40 |
+
|
41 |
+
def generate_img():
|
42 |
+
link = link_text.value
|
43 |
+
qr_img = generate_qr_code(link)
|
44 |
+
img = download_qr_code(qr_img)
|
45 |
+
myimg.image = img
|
46 |
+
|
47 |
+
#ui
|
48 |
+
ss.write("# QR Code Generator")
|
49 |
+
link_text = ss.text_input("http://www.simplestart.cc", label = "Enter a link:", onchange = text_change, max_width = 600)
|
50 |
+
myimg = ss.image("", title='Generated QR Code', width=300, border= True)
|
51 |
+
|
52 |
+
generate_img()
|
53 |
+
|
54 |
+
ss.link("Download QR Code", myimg.image, file_name = "qr_code.png", mime = "image/png")
|
55 |
+
|
56 |
+
|
57 |
+
ss.space()
|
58 |
+
ss.md('''
|
59 |
+
---
|
60 |
+
### Related References:
|
61 |
+
[Streamlit Re-runs App on Button Click, Delaying Download](https://stackoverflow.com/questions/78045476/streamlit-re-runs-app-on-button-click-delaying-download) (stackoverflow)
|
62 |
+
|
63 |
+
[source code of this demo](https://github.com/readever/simplestart/tree/main/demo/stackoverflow/a%20link%20to%20download%20qrcode) (github)
|
64 |
+
''')
|
pages/002 Bokeh scatter bekoh plot.py
ADDED
@@ -0,0 +1,77 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
### Scatter Plot with Slider C
|
2 |
+
|
3 |
+
# this is demo is based on the topic discussed on stackoverflow
|
4 |
+
# https://stackoverflow.com/questions/78542719/streamlit-slider-will-refresh-the-whole-page-and-go-to-the-top
|
5 |
+
|
6 |
+
import simplestart as ss
|
7 |
+
from bokeh.plotting import figure
|
8 |
+
from bokeh.layouts import column
|
9 |
+
from bokeh.models import ColumnDataSource, CustomJS, Slider
|
10 |
+
from bokeh.resources import CDN
|
11 |
+
from bokeh.embed import file_html, components
|
12 |
+
|
13 |
+
|
14 |
+
### ui
|
15 |
+
ss.md('''
|
16 |
+
#### This example uses a slider outside of Bokeh to change the chart data, combining the smoothness of JavaScript with the convenience of Python.
|
17 |
+
---
|
18 |
+
''')
|
19 |
+
|
20 |
+
# Title for the application
|
21 |
+
ss.write('# Bernoulli Distribution Visualization')
|
22 |
+
|
23 |
+
|
24 |
+
def plot(p=0.5):
|
25 |
+
x = [0, 1]
|
26 |
+
y = [1 - p, p]
|
27 |
+
|
28 |
+
colors = ['#1f77b4', '#ff7f0e']
|
29 |
+
source = ColumnDataSource(data=dict(x=x, y=y, color=colors))
|
30 |
+
source.name = "mysource"
|
31 |
+
|
32 |
+
fig = figure(
|
33 |
+
title="Bernoulli Distribution",
|
34 |
+
x_axis_label="Outcome",
|
35 |
+
y_axis_label="Probability",
|
36 |
+
)
|
37 |
+
|
38 |
+
fig.title.align = "center"
|
39 |
+
|
40 |
+
fig.vbar(x="x", top="y", width=0.5, color="color", source=source)
|
41 |
+
|
42 |
+
return file_html(fig, CDN, "Bernoulli Distribution")
|
43 |
+
|
44 |
+
ss.htmlview(plot())
|
45 |
+
|
46 |
+
def change_value(value):
|
47 |
+
js = f'''
|
48 |
+
const iframe = document.getElementById('myIframe');
|
49 |
+
|
50 |
+
const innerWindow = iframe.contentWindow;
|
51 |
+
|
52 |
+
const bokehDoc = iframe.contentWindow.Bokeh.documents[0];
|
53 |
+
const source = bokehDoc.get_model_by_name("mysource");
|
54 |
+
//console.log("source", source)
|
55 |
+
|
56 |
+
// updata source data
|
57 |
+
var value = {value};
|
58 |
+
source.data['y'] = [1 - value, parseFloat(value)];
|
59 |
+
source.change.emit();
|
60 |
+
'''
|
61 |
+
return js
|
62 |
+
|
63 |
+
|
64 |
+
def slider_change(event):
|
65 |
+
ss.exec_js(change_value(event.value))
|
66 |
+
|
67 |
+
ss.slider('Probability of Success \(p\)', 0.0, 1.0, 0.5, onchange=slider_change, triggerOnUpdate = True)
|
68 |
+
|
69 |
+
|
70 |
+
ss.space()
|
71 |
+
ss.md('''
|
72 |
+
---
|
73 |
+
### Related References:
|
74 |
+
[Streamlit Re-runs App on Button Click, Delaying Download](https://stackoverflow.com/questions/78542719/streamlit-slider-will-refresh-the-whole-page-and-go-to-the-top) (stackoverflow)
|
75 |
+
|
76 |
+
[source code of this demo](https://github.com/readever/simplestart/tree/main/demo/stackoverflow/slider%20outside%20of%20bekoh) (github)
|
77 |
+
''')
|