Spaces:
Sleeping
Sleeping
Upload app.py
Browse files
app.py
CHANGED
|
@@ -1,167 +1,183 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
<
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
.
|
| 89 |
-
|
| 90 |
-
.
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
|
|
|
|
|
|
|
|
|
| 111 |
container.on("click", null);
|
| 112 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 113 |
}
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 125 |
}
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
.attr("cx", 0)
|
| 146 |
-
.attr("cy", 0)
|
| 147 |
-
.attr("r", 60)
|
| 148 |
-
.style({"fill": "white", "cursor": "pointer"});
|
| 149 |
-
|
| 150 |
-
container.append("text")
|
| 151 |
-
.attr("x", 0)
|
| 152 |
-
.attr("y", 15)
|
| 153 |
-
.attr("text-anchor", "middle")
|
| 154 |
-
.text("SPIN")
|
| 155 |
-
.style({"font-weight": "bold", "font-size": "40px"});
|
| 156 |
-
|
| 157 |
-
function rotTween(to){
|
| 158 |
-
var i = d3.interpolate(oldrotation % 360, rotation);
|
| 159 |
-
return function(t){ return "rotate(" + i(t) + ")"; };
|
| 160 |
-
}
|
| 161 |
-
</script>
|
| 162 |
-
</body>
|
| 163 |
-
</html>
|
| 164 |
-
"""
|
| 165 |
-
|
| 166 |
-
# Embed the HTML code into the Streamlit app using st.components.v1.html
|
| 167 |
-
st.components.v1.html(html_code, height=600, width=800)
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
|
| 3 |
+
def create_spin_wheel_app():
|
| 4 |
+
# Set page config
|
| 5 |
+
st.set_page_config(
|
| 6 |
+
page_title="Spin the Wheel!",
|
| 7 |
+
page_icon="π―",
|
| 8 |
+
layout="wide"
|
| 9 |
+
)
|
| 10 |
+
|
| 11 |
+
# Add title and description
|
| 12 |
+
st.title("π― Spin the Wheel!")
|
| 13 |
+
st.write("Click the wheel to spin and win tickets!")
|
| 14 |
+
|
| 15 |
+
# HTML and JavaScript code for the spinning wheel
|
| 16 |
+
html_code = """
|
| 17 |
+
<!DOCTYPE html>
|
| 18 |
+
<html lang="en">
|
| 19 |
+
<head>
|
| 20 |
+
<meta charset="UTF-8">
|
| 21 |
+
<title>Spin the Wheel</title>
|
| 22 |
+
<script src="https://d3js.org/d3.v3.min.js" charset="utf-8"></script>
|
| 23 |
+
<style>
|
| 24 |
+
text {
|
| 25 |
+
font-family: Helvetica, Arial, sans-serif;
|
| 26 |
+
font-size: 19px;
|
| 27 |
+
pointer-events: none;
|
| 28 |
+
}
|
| 29 |
+
#chart {
|
| 30 |
+
position: relative;
|
| 31 |
+
width: 500px;
|
| 32 |
+
height: 500px;
|
| 33 |
+
margin: 0 auto;
|
| 34 |
+
}
|
| 35 |
+
#question {
|
| 36 |
+
text-align: center;
|
| 37 |
+
margin-top: 20px;
|
| 38 |
+
}
|
| 39 |
+
#question h1 {
|
| 40 |
+
font-size: 32px;
|
| 41 |
+
font-weight: bold;
|
| 42 |
+
font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
|
| 43 |
+
margin: 0;
|
| 44 |
+
padding: 10px;
|
| 45 |
+
}
|
| 46 |
+
</style>
|
| 47 |
+
</head>
|
| 48 |
+
<body>
|
| 49 |
+
<div id="chart"></div>
|
| 50 |
+
<div id="question"><h1></h1></div>
|
| 51 |
+
|
| 52 |
+
<script>
|
| 53 |
+
var padding = {top:20, right:40, bottom:0, left:0},
|
| 54 |
+
w = 360 - padding.left - padding.right,
|
| 55 |
+
h = 360 - padding.top - padding.bottom,
|
| 56 |
+
r = Math.min(w, h) / 2,
|
| 57 |
+
rotation = 0,
|
| 58 |
+
oldrotation = 0,
|
| 59 |
+
picked = 100000,
|
| 60 |
+
oldpick = [],
|
| 61 |
+
color = d3.scale.category20();
|
| 62 |
+
|
| 63 |
+
var data = [
|
| 64 |
+
{"label": "ποΈ", "value": 1, "question": "1 Ticket"},
|
| 65 |
+
{"label": "ποΈ", "value": 2, "question": "2 Tickets"},
|
| 66 |
+
{"label": "ποΈ", "value": 3, "question": "3 Tickets"},
|
| 67 |
+
{"label": "ποΈ", "value": 4, "question": "4 Tickets"},
|
| 68 |
+
{"label": "ποΈ", "value": 5, "question": "5 Tickets"},
|
| 69 |
+
{"label": "ποΈ", "value": 6, "question": "6 Tickets"},
|
| 70 |
+
{"label": "ποΈ", "value": 7, "question": "7 Tickets"},
|
| 71 |
+
{"label": "ποΈ", "value": 8, "question": "8 Tickets"},
|
| 72 |
+
{"label": "ποΈ", "value": 9, "question": "9 Tickets"},
|
| 73 |
+
{"label": "ποΈ", "value": 10, "question": "10 Tickets"}
|
| 74 |
+
];
|
| 75 |
+
|
| 76 |
+
var svg = d3.select('#chart')
|
| 77 |
+
.append("svg")
|
| 78 |
+
.data([data])
|
| 79 |
+
.attr("width", w + padding.left + padding.right)
|
| 80 |
+
.attr("height", h + padding.top + padding.bottom);
|
| 81 |
+
|
| 82 |
+
var container = svg.append("g")
|
| 83 |
+
.attr("class", "chartholder")
|
| 84 |
+
.attr("transform", "translate(" + (w/2 + padding.left) + "," + (h/2 + padding.top) + ")");
|
| 85 |
+
|
| 86 |
+
var vis = container.append("g");
|
| 87 |
+
|
| 88 |
+
var pie = d3.layout.pie().sort(null).value(function(d){ return 1; });
|
| 89 |
+
|
| 90 |
+
var arc = d3.svg.arc().outerRadius(r);
|
| 91 |
+
|
| 92 |
+
var arcs = vis.selectAll("g.slice")
|
| 93 |
+
.data(pie)
|
| 94 |
+
.enter()
|
| 95 |
+
.append("g")
|
| 96 |
+
.attr("class", "slice");
|
| 97 |
+
|
| 98 |
+
arcs.append("path")
|
| 99 |
+
.attr("fill", function(d, i){ return color(i); })
|
| 100 |
+
.attr("d", function(d){ return arc(d); });
|
| 101 |
+
|
| 102 |
+
arcs.append("text").attr("transform", function(d){
|
| 103 |
+
d.innerRadius = 0;
|
| 104 |
+
d.outerRadius = r;
|
| 105 |
+
d.angle = (d.startAngle + d.endAngle) / 2;
|
| 106 |
+
return "rotate(" + (d.angle * 180 / Math.PI - 90) + ")translate(" + (d.outerRadius - 10) + ")";
|
| 107 |
+
})
|
| 108 |
+
.attr("text-anchor", "end")
|
| 109 |
+
.text(function(d, i){ return data[i].label; });
|
| 110 |
+
|
| 111 |
+
container.on("click", spin);
|
| 112 |
+
|
| 113 |
+
function spin(d){
|
| 114 |
container.on("click", null);
|
| 115 |
+
if(oldpick.length == data.length){
|
| 116 |
+
oldpick = [];
|
| 117 |
+
}
|
| 118 |
+
var ps = 360/data.length,
|
| 119 |
+
pieslice = Math.round(1440/data.length),
|
| 120 |
+
rng = Math.floor((Math.random() * 1440) + 360);
|
| 121 |
+
rotation = (Math.round(rng / ps) * ps);
|
| 122 |
+
picked = Math.round(data.length - (rotation % 360)/ps);
|
| 123 |
+
picked = picked >= data.length ? (picked % data.length) : picked;
|
| 124 |
+
if(oldpick.indexOf(picked) !== -1){
|
| 125 |
+
d3.select(this).call(spin);
|
| 126 |
+
return;
|
| 127 |
+
} else {
|
| 128 |
+
oldpick.push(picked);
|
| 129 |
+
}
|
| 130 |
+
rotation += 90 - Math.round(ps/2);
|
| 131 |
+
vis.transition()
|
| 132 |
+
.duration(3000)
|
| 133 |
+
.attrTween("transform", rotTween)
|
| 134 |
+
.each("end", function(){
|
| 135 |
+
d3.select("#question h1")
|
| 136 |
+
.text(data[picked].question);
|
| 137 |
+
oldrotation = rotation;
|
| 138 |
+
container.on("click", spin);
|
| 139 |
+
});
|
| 140 |
}
|
| 141 |
+
|
| 142 |
+
svg.append("g")
|
| 143 |
+
.attr("transform", "translate(" + (w + padding.left + padding.right) + "," + ((h/2)+padding.top) + ")")
|
| 144 |
+
.append("path")
|
| 145 |
+
.attr("d", "M-" + (r*.15) + ",0L0," + (r*.05) + "L0,-" + (r*.05) + "Z")
|
| 146 |
+
.style({"fill":"black"});
|
| 147 |
+
|
| 148 |
+
container.append("circle")
|
| 149 |
+
.attr("cx", 0)
|
| 150 |
+
.attr("cy", 0)
|
| 151 |
+
.attr("r", 60)
|
| 152 |
+
.style({"fill":"white","cursor":"pointer"});
|
| 153 |
+
|
| 154 |
+
container.append("text")
|
| 155 |
+
.attr("x", 0)
|
| 156 |
+
.attr("y", 15)
|
| 157 |
+
.attr("text-anchor", "middle")
|
| 158 |
+
.text("SPIN")
|
| 159 |
+
.style({"font-weight":"bold", "font-size":"30px"});
|
| 160 |
+
|
| 161 |
+
function rotTween(to) {
|
| 162 |
+
var i = d3.interpolate(oldrotation % 360, rotation);
|
| 163 |
+
return function(t) { return "rotate(" + i(t) + ")"; };
|
| 164 |
}
|
| 165 |
+
</script>
|
| 166 |
+
</body>
|
| 167 |
+
</html>
|
| 168 |
+
"""
|
| 169 |
+
|
| 170 |
+
# Embed the HTML code using st.components.v1.html
|
| 171 |
+
st.components.v1.html(html_code, height=600)
|
| 172 |
+
|
| 173 |
+
# Add instructions
|
| 174 |
+
with st.expander("How to Play"):
|
| 175 |
+
st.write("""
|
| 176 |
+
1. Click the 'SPIN' button in the center of the wheel
|
| 177 |
+
2. Wait for the wheel to stop spinning
|
| 178 |
+
3. See how many tickets you've won!
|
| 179 |
+
4. The wheel can be spun multiple times
|
| 180 |
+
""")
|
| 181 |
+
|
| 182 |
+
if __name__ == "__main__":
|
| 183 |
+
create_spin_wheel_app()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|