mirror of
https://github.com/kennethreitz/replit-py.git
synced 2026-06-05 23:10:18 +00:00
replit online development
This commit is contained in:
@@ -1,13 +1,13 @@
|
||||
from replit import maqpy
|
||||
from replit import flask_tools
|
||||
|
||||
app = maqpy.App(__name__)
|
||||
app = flask_tools.App(__name__)
|
||||
|
||||
|
||||
@app.route("/")
|
||||
@maqpy.authed_ratelimit(
|
||||
@flask_tools.authed_ratelimit(
|
||||
max_requests=1, # Number of requests allowed
|
||||
period=1, # Amount of time before counter resets
|
||||
login_res=maqpy.Page(body=f"Sign in\n{maqpy.sign_in_snippet}"),
|
||||
login_res=flask_tools.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 maqpy
|
||||
from replit import flask_tools
|
||||
|
||||
app = maqpy.App(__name__)
|
||||
app = flask_tools.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 = maqpy.File(__file__)
|
||||
f = flask_tools.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,11 +1,12 @@
|
||||
"""An example of the app.login_wall() feature."""
|
||||
from replit import maqpy
|
||||
from replit.flask_tools import ReplitApp, Link
|
||||
from replit.flask_tools import auth_info
|
||||
|
||||
app = maqpy.App(__name__)
|
||||
app = flask_tools.ReplitApp(__name__)
|
||||
|
||||
|
||||
def login_page() -> str:
|
||||
return "Hello, please sign in to access this page!\n" + maqpy.sign_in_snippet
|
||||
return "Hello, please sign in to access this page!\n" + flask_tools.sign_in_snippet
|
||||
|
||||
|
||||
# this is an example of the arguments, but the exclude kwarg already defaults to this
|
||||
@@ -15,12 +16,12 @@ app.login_wall(exclude=["/"], handler=login_page)
|
||||
|
||||
@app.route("/")
|
||||
def index() -> str:
|
||||
return f"Hello! {maqpy.Link('check out this page', href='/page')}"
|
||||
return f"Hello! {flask_tools.Link('check out this page', href='/page')}"
|
||||
|
||||
|
||||
@app.route("/page")
|
||||
def page() -> str:
|
||||
return f"Hello {maqpy.auth.name}, if you are reading this you signed in."
|
||||
return f"Hello {flask_tools.auth.name}, if you are reading this you signed in."
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
@@ -0,0 +1,16 @@
|
||||
"""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,6 +1,6 @@
|
||||
from replit import maqpy
|
||||
from replit import flask_tools
|
||||
|
||||
app = maqpy.App(__name__)
|
||||
app = flask_tools.App(__name__)
|
||||
|
||||
|
||||
@app.route("/")
|
||||
@@ -1,10 +1,10 @@
|
||||
from replit import maqpy
|
||||
from replit import flask_tools
|
||||
|
||||
app = maqpy.App(__name__)
|
||||
app = flask_tools.App(__name__)
|
||||
|
||||
|
||||
@app.route("/")
|
||||
@maqpy.needs_sign_in(login_res=f"Hello! {maqpy.sign_in_snippet}")
|
||||
@maqpy.needs_sign_in(login_res=f"Hello! {flask_tools.sign_in_snippet}")
|
||||
def index():
|
||||
return "Index function"
|
||||
|
||||
@@ -1,16 +0,0 @@
|
||||
"""A basic example of repl auth."""
|
||||
from replit import maqpy
|
||||
|
||||
app = maqpy.App(__name__)
|
||||
|
||||
|
||||
@app.route("/")
|
||||
def index():
|
||||
if maqpy.signed_in:
|
||||
return "Hello " + maqpy.auth.name
|
||||
else:
|
||||
return maqpy.signin() # optionally: simple.sigin(title="My title")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
app.run()
|
||||
@@ -1,13 +1,12 @@
|
||||
"""The replit python module."""
|
||||
from . import maqpy
|
||||
"""The Replit Python module."""
|
||||
|
||||
from . import flask_tools
|
||||
from . import termutils
|
||||
from .audio import Audio
|
||||
from .database import db
|
||||
|
||||
|
||||
def clear() -> None:
|
||||
"""Clear the terminal."""
|
||||
print("\033[H\033[2J", end="", flush=True)
|
||||
# Backwards compatibility.
|
||||
from .termutils import clear
|
||||
|
||||
|
||||
audio = Audio()
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
"""Make apps quickly in python."""
|
||||
"""Make apps quickly using Python."""
|
||||
|
||||
import os
|
||||
|
||||
import flask
|
||||
@@ -6,7 +7,7 @@ from werkzeug.local import LocalProxy
|
||||
|
||||
from . import files
|
||||
from . import html
|
||||
from .app import App
|
||||
from .app import ReplitApp
|
||||
from .files import File
|
||||
from .html import HTMLElement, Link, Page, Paragraph
|
||||
from .utils import (
|
||||
@@ -1,4 +1,5 @@
|
||||
"""Core of maqpy."""
|
||||
"""Core of flask_tools."""
|
||||
|
||||
from dataclasses import dataclass
|
||||
from functools import wraps
|
||||
from pathlib import Path
|
||||
@@ -10,7 +11,7 @@ from .utils import sign_in
|
||||
|
||||
|
||||
@dataclass
|
||||
class ReplitAuthContext:
|
||||
class ReplitUserContext:
|
||||
"""A dataclass defining a Repl Auth state."""
|
||||
|
||||
user_id: int
|
||||
@@ -19,7 +20,7 @@ class ReplitAuthContext:
|
||||
|
||||
@classmethod
|
||||
def from_headers(cls, headers: dict) -> Any:
|
||||
"""Initialize an instance using the Replit magic headers.
|
||||
"""Initialize an instance using the Replit identification headers.
|
||||
|
||||
Args:
|
||||
headers (dict): A dictionary of headers received
|
||||
@@ -34,8 +35,8 @@ class ReplitAuthContext:
|
||||
)
|
||||
|
||||
@property
|
||||
def signed_in(self) -> bool:
|
||||
"""Check whether the user is signed in with repl auth.
|
||||
def is_authenticated(self) -> bool:
|
||||
"""Check whether the user is authenticated in with Replit Auth.
|
||||
|
||||
Returns:
|
||||
bool: whether or not the authentication is activated.
|
||||
@@ -43,7 +44,7 @@ class ReplitAuthContext:
|
||||
return bool(self.name)
|
||||
|
||||
|
||||
class Request(flask.Request):
|
||||
class ReplitRequest(flask.Request):
|
||||
"""Represents a client request."""
|
||||
|
||||
def __init__(self, *args: Any, **kwargs: Any) -> None:
|
||||
@@ -57,23 +58,23 @@ class Request(flask.Request):
|
||||
self.update_auth()
|
||||
|
||||
def update_auth(self) -> None:
|
||||
"""Update the auth property to be a ReplitAuthContext."""
|
||||
self.auth = ReplitAuthContext.from_headers(self.headers)
|
||||
"""Update the user_info property to be a ReplitUserContext."""
|
||||
self.user_info = ReplitUserContext.from_headers(self.headers)
|
||||
|
||||
@property
|
||||
def signed_in(self) -> bool:
|
||||
"""Check whether the user is signed in with repl auth.
|
||||
def is_authenticated(self) -> bool:
|
||||
"""Check whether the user is authenticated with Replit.
|
||||
|
||||
Returns:
|
||||
bool: Whether or not the user is signed in
|
||||
"""
|
||||
return self.auth.signed_in
|
||||
return self.user_info.is_authenticated
|
||||
|
||||
|
||||
class App(flask.Flask):
|
||||
class ReplitApp(flask.Flask):
|
||||
"""Represents a web application."""
|
||||
|
||||
request_class = Request
|
||||
request_class = ReplitRequest
|
||||
|
||||
def __init__(
|
||||
self, import_name: str, nice_jinja: bool = True, **kwargs: Any
|
||||
@@ -154,7 +155,7 @@ class App(flask.Flask):
|
||||
)
|
||||
|
||||
def _run(self, *args: Any, **kwargs: Any) -> Any:
|
||||
"""Interface with the underlying flask instance's run function.
|
||||
"""Interface with the underlying Flask instance's run function.
|
||||
|
||||
Args:
|
||||
args (Any): The arguments to be passed to the superclass' run method.
|
||||
@@ -211,4 +212,4 @@ class App(flask.Flask):
|
||||
port=port,
|
||||
debug=True,
|
||||
extra_files=watch_files,
|
||||
)
|
||||
)
|
||||
@@ -1,4 +1,5 @@
|
||||
"""Utitilities for interacting with static files."""
|
||||
"""Utilities for interacting with static files."""
|
||||
|
||||
import flask
|
||||
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
"""Python object representations of HTML."""
|
||||
|
||||
from abc import ABC
|
||||
|
||||
import flask
|
||||
@@ -1,4 +1,5 @@
|
||||
"""Utitilities to make development easier."""
|
||||
|
||||
from functools import wraps
|
||||
import time
|
||||
from typing import Any, Callable, Iterable, Optional, Union
|
||||
@@ -1,6 +1,5 @@
|
||||
"""Pure Python ANSI Color Escape Code generator."""
|
||||
|
||||
|
||||
import colorsys
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
from flask import Flask
|
||||
app = Flask(__name__)
|
||||
|
||||
@app.route('/')
|
||||
def hello_world():
|
||||
return 'Hello, World!'
|
||||
|
||||
app.run(host='0.0.0.0')
|
||||
@@ -3,8 +3,8 @@
|
||||
import os
|
||||
import unittest
|
||||
|
||||
from replit.database import AsyncDatabase, AsyncJSONKey, Database
|
||||
import requests
|
||||
from replit.database import AsyncDatabase, AsyncJSONKey, Database
|
||||
|
||||
|
||||
class TestAsyncDatabase(unittest.IsolatedAsyncioTestCase):
|
||||
|
||||
Reference in New Issue
Block a user