/s/flask_tools/web

This commit is contained in:
2021-01-06 21:48:28 -05:00
parent 0d7e63c1ba
commit 9562b1faec
17 changed files with 57 additions and 71 deletions
-16
View File
@@ -1,16 +0,0 @@
"""A basic example of repl auth."""
from replit import flask_tools
app = flask_tools.App(__name__)
@app.route("/")
def index():
if flask_tools.signed_in:
return "Hello " + flask_tools.auth.name
else:
return flask_tools.signin() # optionally: simple.sigin(title="My title")
if __name__ == "__main__":
app.run()
@@ -1,13 +1,13 @@
from replit import flask_tools
from replit import web
app = flask_tools.App(__name__)
app = web.App(__name__)
@app.route("/")
@flask_tools.authed_ratelimit(
@web.authed_ratelimit(
max_requests=1, # Number of requests allowed
period=1, # Amount of time before counter resets
login_res=flask_tools.Page(body=f"Sign in\n{maqpy.sign_in_snippet}"),
login_res=web.Page(body=f"Sign in\n{maqpy.sign_in_snippet}"),
get_ratelimited_res=(lambda left: f"Too many requests, try again after {left} sec"),
)
def index():
@@ -1,13 +1,13 @@
from replit import flask_tools
from replit import web
app = flask_tools.App(__name__)
app = web.App(__name__)
@app.route("/")
def index():
# Once a file is loaded once, it is cached in memory unless you pass the no_cache
# kwarg to the File constructor
f = flask_tools.File(__file__)
f = web.File(__file__)
# Tell the browser its just text, not a webpage
# we can edit the headers directly because the file is just a flask.Response object
f.headers["Content-Type"] = "text/plain"
@@ -1,12 +1,12 @@
"""An example of the app.login_wall() feature."""
from replit.flask_tools import ReplitApp, Link
from replit.flask_tools import auth_info
from replit.web import ReplitApp, Link
from replit.web import auth_info
app = flask_tools.ReplitApp(__name__)
app = web.ReplitApp(__name__)
def login_page() -> str:
return "Hello, please sign in to access this page!\n" + flask_tools.sign_in_snippet
return "Hello, please sign in to access this page!\n" + web.sign_in_snippet
# this is an example of the arguments, but the exclude kwarg already defaults to this
@@ -16,12 +16,12 @@ app.login_wall(exclude=["/"], handler=login_page)
@app.route("/")
def index() -> str:
return f"Hello! {flask_tools.Link('check out this page', href='/page')}"
return f"Hello! {web.Link('check out this page', href='/page')}"
@app.route("/page")
def page() -> str:
return f"Hello {flask_tools.auth.name}, if you are reading this you signed in."
return f"Hello {web.auth.name}, if you are reading this you signed in."
if __name__ == "__main__":
+16
View File
@@ -0,0 +1,16 @@
"""A basic example of repl auth."""
from replit import web
app = web.App(__name__)
@app.route("/")
def index():
if web.signed_in:
return "Hello " + web.auth.name
else:
return web.signin() # optionally: simple.sigin(title="My title")
if __name__ == "__main__":
app.run()
@@ -1,6 +1,6 @@
from replit import flask_tools
from replit import web
app = flask_tools.App(__name__)
app = web.App(__name__)
@app.route("/")
@@ -29,14 +29,14 @@ def onerror(missing):
@app.route("/form", methods=["POST"])
@maqpy.needs_params("param", onerror=onerror)
@web.needs_params("param", onerror=onerror)
def form(param):
return f"The value of param is: {param}"
@app.route("/query")
# source can be form, query, or a dictionary
@maqpy.needs_params("q", src="query", onerror=(lambda p: f"Need query param {p}"))
@web.needs_params("q", src="query", onerror=(lambda p: f"Need query param {p}"))
def query(q):
return f"The query param is: {q}"
@@ -1,17 +1,17 @@
from replit import flask_tools
from replit import web
app = flask_tools.App(__name__)
app = web.App(__name__)
@app.route("/")
@maqpy.needs_sign_in(login_res=f"Hello! {flask_tools.sign_in_snippet}")
@web.needs_sign_in(login_res=f"Hello! {web.sign_in_snippet}")
def index():
return "Index function"
# needs_signin can also be called with no args
@app.route("/test")
@maqpy.needs_sign_in
@web.needs_sign_in
def test():
return "Test function"
+1 -2
View File
@@ -1,10 +1,9 @@
"""The Replit Python module."""
from . import flask_tools
from . import web
from . import termutils
from .audio import Audio
from .database import db
from .users import ReplitUser
# Backwards compatibility.
+1 -5
View File
@@ -331,11 +331,7 @@ class Audio:
LoopCount=loop_count,
Volume=volume,
Type=str(ReaderType.tone),
Args=RequestArgs(
WaveType=wave_type,
Pitch=pitch,
Seconds=duration,
),
Args=RequestArgs(WaveType=wave_type, Pitch=pitch, Seconds=duration,),
)
with open("/tmp/audio", "w") as f:
+2 -8
View File
@@ -84,10 +84,7 @@ class AsyncJSONKey:
return await self._error("Invalid JSON data read", read)
if not self._is_valid_type(data):
return await self._error(
self._type_mismatch_msg(data),
read,
)
return await self._error(self._type_mismatch_msg(data), read,)
return data
async def _error(self, error: str, read: str) -> JSON_TYPE:
@@ -212,10 +209,7 @@ class JSONKey(AsyncJSONKey):
data = read
if not self._is_valid_type(data):
return self._error(
self._type_mismatch_msg(data),
read,
)
return self._error(self._type_mismatch_msg(data), read,)
return data
def _error(self, error: str, read: str) -> JSON_TYPE:
+8 -10
View File
@@ -126,16 +126,14 @@ class Bit:
self.bg = f"\033[48;5;{value}m"
attributes = (
{ # use only repl.it supported ansi codes. Codes such as blink do not work.
"reset": 0,
"bold": 1,
"faint": 2,
"italic": 3,
"underline": 4,
"highlight": 7,
}
)
attributes = { # use only repl.it supported ansi codes. Codes such as blink do not work.
"reset": 0,
"bold": 1,
"faint": 2,
"italic": 3,
"underline": 4,
"highlight": 7,
}
class Attr:
@@ -93,11 +93,7 @@ class ReplitApp(flask.Flask):
self.jinja_env.trim_blocks = True
self.jinja_env.lstrip_blocks = True
def login_wall(
self,
exclude: Set[str] = ("/",),
handler: Callable = None,
) -> None:
def login_wall(self, exclude: Set[str] = ("/",), handler: Callable = None,) -> None:
"""Require users to be logged-in on all pages.
Args:
+7 -4
View File
@@ -1,8 +1,11 @@
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello_world():
return 'Hello, World!'
app.run(host='0.0.0.0')
@app.route("/")
def hello_world():
return "Hello, World!"
app.run(host="0.0.0.0")