Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,157 +1,65 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
import
|
| 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 |
-
# Filter the rows so we only include the desired species
|
| 67 |
-
return df[df["Species"].isin(input.species())]
|
| 68 |
-
|
| 69 |
-
@output
|
| 70 |
-
@render.plot
|
| 71 |
-
def scatter():
|
| 72 |
-
"""Generates a plot for Shiny to display to the user"""
|
| 73 |
-
|
| 74 |
-
# The plotting function to use depends on whether margins are desired
|
| 75 |
-
plotfunc = sns.jointplot if input.show_margins() else sns.scatterplot
|
| 76 |
-
|
| 77 |
-
plotfunc(
|
| 78 |
-
data=filtered_df(),
|
| 79 |
-
x=input.xvar(),
|
| 80 |
-
y=input.yvar(),
|
| 81 |
-
palette=palette,
|
| 82 |
-
hue="Species" if input.by_species() else None,
|
| 83 |
-
hue_order=species,
|
| 84 |
-
legend=False,
|
| 85 |
-
)
|
| 86 |
-
|
| 87 |
-
@output
|
| 88 |
-
@render.ui
|
| 89 |
-
def value_boxes():
|
| 90 |
-
df = filtered_df()
|
| 91 |
-
|
| 92 |
-
def penguin_value_box(title: str, count: int, bgcol: str, showcase_img: str):
|
| 93 |
-
return x.ui.value_box(
|
| 94 |
-
title,
|
| 95 |
-
count,
|
| 96 |
-
{"class_": "pt-1 pb-0"},
|
| 97 |
-
showcase=x.ui.bind_fill_role(
|
| 98 |
-
ui.tags.img(
|
| 99 |
-
{"style": "object-fit:contain;"},
|
| 100 |
-
src=showcase_img,
|
| 101 |
-
),
|
| 102 |
-
item=True,
|
| 103 |
-
),
|
| 104 |
-
theme_color=None,
|
| 105 |
-
style=f"background-color: {bgcol};",
|
| 106 |
-
height="90px",
|
| 107 |
-
full_screen=True,
|
| 108 |
-
)
|
| 109 |
-
|
| 110 |
-
if not input.by_species():
|
| 111 |
-
return penguin_value_box(
|
| 112 |
-
"Penguins",
|
| 113 |
-
len(df.index),
|
| 114 |
-
bg_palette["default"],
|
| 115 |
-
# Artwork by @allison_horst
|
| 116 |
-
showcase_img="penguins.png",
|
| 117 |
-
)
|
| 118 |
-
|
| 119 |
-
value_boxes = [
|
| 120 |
-
penguin_value_box(
|
| 121 |
-
name,
|
| 122 |
-
len(df[df["Species"] == name]),
|
| 123 |
-
bg_palette[name],
|
| 124 |
-
# Artwork by @allison_horst
|
| 125 |
-
showcase_img=f"{name}.png",
|
| 126 |
-
)
|
| 127 |
-
for name in species
|
| 128 |
-
# Only include boxes for _selected_ species
|
| 129 |
-
if name in input.species()
|
| 130 |
-
]
|
| 131 |
-
|
| 132 |
-
return x.ui.layout_column_wrap(1 / len(value_boxes), *value_boxes)
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
# "darkorange", "purple", "cyan4"
|
| 136 |
-
colors = [[255, 140, 0], [160, 32, 240], [0, 139, 139]]
|
| 137 |
-
colors = [(r / 255.0, g / 255.0, b / 255.0) for r, g, b in colors]
|
| 138 |
-
|
| 139 |
-
palette: Dict[str, Tuple[float, float, float]] = {
|
| 140 |
-
"Adelie": colors[0],
|
| 141 |
-
"Chinstrap": colors[1],
|
| 142 |
-
"Gentoo": colors[2],
|
| 143 |
-
"default": sns.color_palette()[0], # type: ignore
|
| 144 |
-
}
|
| 145 |
-
|
| 146 |
-
bg_palette = {}
|
| 147 |
-
# Use `sns.set_style("whitegrid")` to help find approx alpha value
|
| 148 |
-
for name, col in palette.items():
|
| 149 |
-
# Adjusted n_colors until `axe` accessibility did not complain about color contrast
|
| 150 |
-
bg_palette[name] = mpl_colors.to_hex(sns.light_palette(col, n_colors=7)[1]) # type: ignore
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
app = App(
|
| 154 |
-
app_ui,
|
| 155 |
-
server,
|
| 156 |
-
static_assets=str(www_dir),
|
| 157 |
-
)
|
|
|
|
| 1 |
+
import flask # for the UI and API
|
| 2 |
+
import time # for rate limiting
|
| 3 |
+
import json # for listing, CORRECTLY
|
| 4 |
+
|
| 5 |
+
app = flask.Flask(__name__)
|
| 6 |
+
# woah, VS, it's python, not whatever that down arrow is
|
| 7 |
+
|
| 8 |
+
vars = {} # a dictionary of variables
|
| 9 |
+
ips = {} # a dictionary of IPs and the last time they made a request
|
| 10 |
+
|
| 11 |
+
# get a variable
|
| 12 |
+
@app.route('/get/<var>')
|
| 13 |
+
def get(var):
|
| 14 |
+
try:
|
| 15 |
+
global ips
|
| 16 |
+
ips[flask.request.remote_addr] = time.time()
|
| 17 |
+
global vars
|
| 18 |
+
return vars[var]
|
| 19 |
+
except Exception as e:
|
| 20 |
+
return str(e), 404 # we all know this means "not found"
|
| 21 |
+
# set a variable
|
| 22 |
+
@app.route('/set/<var>/<val>')
|
| 23 |
+
def set(var, val):
|
| 24 |
+
try:
|
| 25 |
+
global ips
|
| 26 |
+
ips[flask.request.remote_addr] = time.time()
|
| 27 |
+
global vars
|
| 28 |
+
vars[var] = val
|
| 29 |
+
return vars[var] # return the value of the variable, just in case it fails
|
| 30 |
+
except Exception as e:
|
| 31 |
+
return str(e), 404
|
| 32 |
+
# delete a variable, making it return 404
|
| 33 |
+
@app.route('/del/<var>')
|
| 34 |
+
def delete(var):
|
| 35 |
+
try:
|
| 36 |
+
global vars
|
| 37 |
+
del vars[var]
|
| 38 |
+
global ips
|
| 39 |
+
ip = flask.request.remote_addr
|
| 40 |
+
if ip not in ips:
|
| 41 |
+
ips[ip] = 0
|
| 42 |
+
if time.time() - ips[ip] < 0.1:
|
| 43 |
+
time.sleep(1) # rate limiting (10 requests per second, but lower if you're exceeding it)
|
| 44 |
+
# should you use a scratch project and port it, you should be fine so long as you abide by their rate limits
|
| 45 |
+
# otherwise, this is significantly more strict, it's kind of just a "don't wipe the database" thing
|
| 46 |
+
return vars[var], 429 # too many requests, but also success
|
| 47 |
+
return vars[var]
|
| 48 |
+
except Exception as e:
|
| 49 |
+
return str(e), 404
|
| 50 |
+
# list all variables
|
| 51 |
+
@app.route('/')
|
| 52 |
+
def list():
|
| 53 |
+
# this uses ACTUAL JSON
|
| 54 |
+
# NOT the fake json used by joecooldo
|
| 55 |
+
# (he makes a cool library)
|
| 56 |
+
# "error: json does not allow single quotes (')"
|
| 57 |
+
global vars
|
| 58 |
+
return json.dumps(vars)
|
| 59 |
+
# list ips
|
| 60 |
+
@app.route('/ips')
|
| 61 |
+
def listips():
|
| 62 |
+
global ips
|
| 63 |
+
return json.dumps(ips)
|
| 64 |
+
# run the app
|
| 65 |
+
app.run(port=7860)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|