Coder Social home page Coder Social logo

replit / replit-py Goto Github PK

View Code? Open in Web Editor NEW
157.0 43.0 57.0 3.89 MB

A helpful Python package that helps you build excellent things inside Repls! ๐Ÿ’ป

Home Page: https://replit-py.readthedocs.io

License: ISC License

Python 100.00%
replit python-library api-wrappers key-value-store database education repl

replit-py's Introduction

>>> import replit

Run on Repl.it pypi: replit Read the Docs

This repository is the home for the replit Python package, which provides:

  • A fully-featured database client for Replit DB.
  • Tools and utilities for Flask Web Development, including an interface to Replit's User Authetication service
  • Replit user profile metadata retrieval (more coming here!).

Open Source License

This library is licensed under the ISC License and is free for you to use, change, or even profit from!

Setup

With poetry already setup, you can set up the repl for development with:

poetry install

Continuous Integration

Running the database unittests tests/test_database.py depends on the repl https://replit.com/@util/database-test-jwt. You'll have access to this repl if you are a Replit employee. There's a secret contained in that repl which you'll have to set as an environment variable in order to run the unittests. Once you've done that, run the tests with:

poetry run python -m unittest

replit-py's People

Contributors

bd103 avatar bekishore avatar blast-hardcheese avatar brenoafb avatar cdmistman avatar dainfloop avatar dependabot[bot] avatar dev380 avatar donno2048 avatar firepup6500 avatar jus-codin avatar kennethreitz avatar lhchavez avatar noneofyourbusiness1415252 avatar piemadd avatar populated avatar potentialstyx avatar ryantm avatar scoder12 avatar sergeichestakov avatar techpixel avatar thewchan avatar ttury avatar ykdojo avatar zabot avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

replit-py's Issues

[question] Repl-py-console

Why does The repl console require you to use this when you don't need it nor you want it because your using nothing in it , not even dependencies from it

Cannot get or delete keys which contain question marks '?'

Steps to reproduce:
An example Repl.it can be found here: https://repl.it/@Ripp/CannotDeleteExample

  1. Store a key/value in the database with a question mark in the key
    db["test ? with mark"] = 1
  2. Try to retrive or delete the key:
    print(db["test ? with mark"]) or
    del db["test ? with mark"]

Expected:
They key is retrived/deleted as apporiate

Actual:
A KeyError is raised, however the key is present as can be verified by inspecting keys (db.keys())

Work around:
The user can pre escape the ? character to %3F, after which it works fine.

Missing flask feature: from Flask import Response

I was running into MIME type issues. I could solve them fine in flask using:
Response("Response Text", mimetype="text/css")
I can't do this in the fork of flask included in this module.

Edit: My import gives an error when like this:
from replit.web import Response

The db breaks with certain types of keys (encoding issues?)

Here's the repl that shows this bug: https://replit.com/@ykdojo/bug-report#main.py

When you do:

from replit import db
db['%E2'] = 3
print("db['%E2']: ", db['%E2'])

You get:

raise HTTPError(http_error_msg, response=self)
requests.exceptions.HTTPError: 400 Client Error: Bad Request for url: https://kv.replit.com/v0/eyJhbGciOiJIUzUxMiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE2MTUyNzg5ODYsImlhdCI6MTYxNTE2NzM4NiwiaXNzIjoiY29ubWFuIiwiZGF0YWJhc2VfaWQiOiIwMDNjMDIyNC03NjAzLTRkMDUtODk5Zi1iYzEzNTg3NWJiMzcifQ.NXV7go-7vWy5A7RTC0XMjF0JqhAyrAJElst_Pd5dWRjxoLC3d-wrXe_ftKScf1I_m5GZrVUl2abkBPGt6LufAA/%E2

Retrieving float value from DB does not work

Reproduction:

from replit import db
db["new_key"] = 1.234
v = db["new_key"]
print(v)

Expected Result: prints 1.234
Actual result:

Traceback (most recent call last):
  File "main.py", line 5, in <module>
    value = db['new_key']
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/replit/database/database.py", line 436, in __getitem__
    return item_to_observed(_get_set_cb(self, key), val)
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/replit/database/database.py", line 396, in item_to_observed
    raise TypeError(f"Unexpected type {type(item).__name__!r}")
TypeError: Unexpected type 'float'

Error when defining replit key

It works successfully on replit, but when I use visual studio code, it'll

'NoneType' object does not support item assignment

replit thinking "telebot" is another package and downloading the wrong one

I tried getting the bot to run on repl.it, but when having import telebot in the code, replit thinks that I want to use this package, instead of pyTelegramBotAPI. So, when running the program, replit automatically installs and starts trying to use that other package in the program, leading it to fail when calling message_handler, an attribute of the TeleBot class in the pyTelegramBotAPI package, but not in telebot

Example code:

import telebot
import os

bot = telebot.TeleBot(os.environ["token"])

@bot.message_handler(commands=["start"])
def hi(message):
    bot.send_message(chat_id=message.chat.id, text="hello there, this is an example"

bot.infinity_polling()

Error:
image_2022-03-17_16-44-08

I am writing import telebot here instead of import pyTelegramBotAPI because:

  1. It says so in the docs of the pyTelegramBotAPI package
  2. It gives a ModuleNotFound error if I write import pyTelegramBotAPI

replit-py not compatible with Flask 3.0.0

https://www.reddit.com/r/replit/s/7lOFt5gL3V

Post content:

Has anyone run into an issue where they cannot import the latest version of Flask and Replit. When I try to import flask I get this error:

Because replit (3.3.2) depends on Flask (>=2.0.0,<3.0.0)

and no versions of replit match >3.3.2,<4.0.0, replit (>=3.3.2,<4.0.0) requires Flask (>=2.0.0,<3.0.0).

So, because python-template depends on both replit (^3.3.2) and flask (^3.0.0), version solving failed.

exit status 1

I've tried installing a previous version of flask but then when I try and start the server with this code:

from replit import web, db

from flask import Flask

app = Flask(__name__)

web.run(app)

โ€‹

I get the following error:

Traceback (most recent call last):

File "/home/runner/IntrepidAltruisticMultiprocessing/main.py", line 6, in <module>

replit.web.run(app)

File "/home/runner/IntrepidAltruisticMultiprocessing/.pythonlibs/lib/python3.10/site-packages/replit/web/app.py", line 62, in run

if change_encoder and app.json_encoder is flask.json.JSONEncoder:

AttributeError: 'Flask' object has no attribute 'json_encoder'

Any help would be much appreciated!

Async giving errors

I was storing large amounts of data, and it started getting too laggy. So I tried to implement the async database with this code:

from replit.database import AsyncDatabase

db = AsyncDatabase(os.environ.get("REPLIT_DB_URL"))

However, just doing await db.keys() is giving me this error:

[2021-10-24 21:09:26 +0000] [8570] [ERROR] Exception occurred while handling uri: 'http://flips.ml/history'
Traceback (most recent call last):
  File "handle_request", line 83, in handle_request
    class Sanic(BaseSanic, metaclass=TouchUpMeta):
  File "main.py", line 281, in get_history
    keys = sorted(list(await db.keys()))
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/replit/database/database.py", line 208, in keys
    return await self.list("")
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/replit/database/database.py", line 178, in list
    async with self.sess.get(self.db_url, params=params) as response:
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/aiohttp/client.py", line 1117, in __aenter__
    self._resp = await self._coro
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/aiohttp/client.py", line 448, in _request
    with timer:
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/aiohttp/helpers.py", line 635, in __enter__
    raise RuntimeError(
RuntimeError: Timeout context manager should be used inside a task

How could I fix this?

AttributeError: 'Database' object has no attribute '_refresh_timer'

i keep getting this error on running http db calls with replit db, full traceback:
Exception in thread Thread-2:
Traceback (most recent call last):
File "C:\Users\Chiranjeevi\AppData\Local\Programs\Python\Python312\Lib\threading.py", line 1073, in _bootstrap_inner
self.run()
File "C:\Users\Chiranjeevi\AppData\Local\Programs\Python\Python312\Lib\threading.py", line 1431, in run
self.function(*self.args, **self.kwargs)
File "C:\Users\Chiranjeevi\AppData\Local\Programs\Python\Python312\Lib\site-packages\replit\database\database.py", line 570, in _watchdog
return self.close()
^^^^^^^^^^^^
File "C:\Users\Chiranjeevi\AppData\Local\Programs\Python\Python312\Lib\site-packages\replit\database\database.py", line 763, in close
if self._refresh_timer:
^^^^^^^^^^^^^^^^^^^
AttributeError: 'Database' object has no attribute '_refresh_timer'

replit db error

[2021-06-06 17:11:50,692] ERROR in app: Exception on /signup [GET]
Traceback (most recent call last):
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/flask/app.py", line 2070, in wsgi_app
    response = self.full_dispatch_request()
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/flask/app.py", line 1515, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/flask/app.py", line 1513, in full_dispatch_request
    rv = self.dispatch_request()
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/flask/app.py", line 1499, in dispatch_request
    return self.ensure_sync(self.view_functions[rule.endpoint])(**req.view_args)
  File "main.py", line 80, in signup
    user.signup()
  File "main.py", line 21, in signup
    db[self.name] = self.data
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/replit/database/database.py", line 484, in __setitem__
    self.set(key, value)
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/replit/database/database.py", line 493, in set
    self.set_raw(key, _dumps(value))
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/replit/database/database.py", line 56, in dumps
    return json.dumps(val, separators=(",", ":"), cls=DBJSONEncoder)
  File "/usr/lib/python3.8/json/__init__.py", line 234, in dumps
    return cls(
  File "/usr/lib/python3.8/json/encoder.py", line 199, in encode
    chunks = self.iterencode(o, _one_shot=True)
  File "/usr/lib/python3.8/json/encoder.py", line 257, in iterencode
    return _iterencode(o, 0)
ValueError: Circular reference detected

looping doesn't work

I'm trying to loop music and it only plays once. I also went to a program that can adjust volume and how many times it loops. when I add loop to it, it only plays once.

Bug/Typo - millaseconds is an invalid keyword argument for new (re-open)

Hi

In trying to use source.duration in the audio library,

  File "/home/runner/Musical-Dice/venv/lib/python3.10/site-packages/replit/audio/__init__.py", line 136, in duration
    return timedelta(millaseconds=self.__payload["Duration"])
TypeError: 'millaseconds' is an invalid keyword argument for __new__()

I saw that issue #6 (#6) was closed in 2020 as fixed, but I see in the source code

    @property
    def duration(self) -> timedelta:
        """The duration of the source."""
        return timedelta(millaseconds=self.__payload["Duration"])

Thank you for your assistance!

AsyncDatabase RuntimeWarning: coroutine 'ClientSession.close' was never awaited

Using AsyncDatabase results in the following warning

/opt/virtualenvs/python3/lib/python3.8/site-packages/replit/database/database.py:80: RuntimeWarning: coroutine 
'ClientSession.close' was never awaited
  self.sess.close()

This can be replicated with the following code:

import asyncio, os, replit
async def main():
  async with replit.AsyncDatabase(os.environ["REPLIT_DB_URL"]) as db:
    print(await db.keys())
    
asyncio.run(main())

This has previously been briefly mentioned in #115, but no fixes were done.
This should be easily fixed by adding await before the self.sess.close() in https://github.com/replit/replit-py/blob/master/src/replit/database/database.py#L80

Should I submit a pull request, or is there something I'm missing out?

[Request] Update dictionary key-value pairs in repl db without updating the entire dictionary

I would like to be able to do something like this:

from replit import db
db["test"] = {"a": "hello"}
# I want to do this.
db["test"]["a"] = "world"
print(db["test"])

And get the following output:

{"a": "world"}

But instead I will get:

{"a": "hello"}

The following code would work:

from replit import db
db["test"] = {"a": "hello"}
# I must use a temporary variable and then update the entire dictionary in the database.
temp = db["test"]
temp["a"] = "world"
db["test"] = temp
print(db["test"])

Bug/Typo - millaseconds is an invalid keyword argument for new

Traceback (most recent call last):
  File "main.py", line 105, in <module>
    duration = playingNow.duration.total_seconds()
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/replit/audio/__init__.py", line 131, in duration
    return timedelta(millaseconds=self.__payload["Duration"])
TypeError: 'millaseconds' is an invalid keyword argument for __new__()

I tried to get the duration but I encountered an error. Seems like a typo: millaseconds should be spelled milliseconds.

AttributeError: 'Request' object has no attribute 'is_authenticated'

Using replit.web, I get this error trying to use the @web.authenticated method.

Screenshot 2021-08-04 at 16 39 24

Code:

import flask
from replit import web, db

app = flask.Flask(__name__)

users = web.UserStore()

@app.route("/")
@web.authenticated
def index():
  hits = users.current.get("hits", 0) + 1
  users.current["hits"] = hits
  return f"You have visited this page {hits} times"

app.run(port=3000,host='0.0.0.0',debug=True)

Using replit database on a different account doesnt work

I am trying to make an email verification system (which works) on a different account, but I cant hide the email and password without being interrupted by this error.

TypeError: 'NoneType' object is not subscriptable

The code I am using, and tried.

Replit.DB

replit.db['email'] = ""
replit.db['password'] = ""

OS

email    = os.environ['EMAIL']
password = os.environ['PASSWORD']

Accessing the DB from outside repl.it

Hey!

I was making an app, and I wanted to use the repl db to save data... The app would be an Android app, and not a repl.it app. Is there a way to access the db from the Android app?

Thanks in Advance!

replit.web using usernames as keys

From what I can see, the replit.web thingy uses usernames as keys for its database. The problem with that is that usernames can change on replit so if someone were to change their username, they would lose all their data on any sites which used this package. If replauth gives you user ids then you should probably use them instead so there is no chance of users losing data due to changing their username.

Replit audio error

Problem:
When using the audio module from this package, a FileNotFoundError occurs

Code sample:

from replit import audio

x = audio.play_file("flashback.mp3")
print(x)

Error:
Screenshot_20230403-195442_Replit

clear() doesn't t work

hello i add replit into my py charm and the function clear() not working for me at all

Locked out of my own database

I got an error randomly when using replit db that I am forbidden from using it!

File "./main.py", line 362, in typewriterSet
    db[f"{owner}.typespeed"] = speed
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/replit/database/database.py", line 491, in __setitem__
    self.set(key, value)
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/replit/database/database.py", line 500, in set
    self.set_raw(key, _dumps(value))
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/replit/database/database.py", line 509, in set_raw
    self.set_bulk_raw({key: value})
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/replit/database/database.py", line 527, in set_bulk_raw
    r.raise_for_status()
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/requests/models.py", line 953, in raise_for_status
    raise HTTPError(http_error_msg, response=self)
requests.exceptions.HTTPError: 403 Client Error: Forbidden for url: REPLIT_DB_URL

Where REPLIT_DB_URL is my database URL.
This is what I see when I view my database link through a browser tab:
invalid token: token is expired by 1h40m42s.
The time refreshes every time I refresh the page.
It doesn't happen in the spotlight page; just in the workspace.
image

Error Working with DateTime UTC

๐Ÿ‘‹ hi there, I'm giving replit db a shot and ran into an error saving utc timestamps.

Steps to reproduce:

from replit import db
import datetime

db['test'] = datetime.datetime.utcnow()

throws an exception: ValueError: Circular reference detected

I was able to solve by converting the timestamp to a string:

db['test'] = str(datetime.datetime.utcnow())

Took me longer than I'd like to admit to debug as I rarely work with Python ๐Ÿ˜… . If this is expected behavior feel free too close.

Thanks for making such a cool tool!

Can't use `web.auth.name` in view parameters

Here's my function throwing the issue:

@app.route("/<user>")
@web.authenticated
def user(user: str = web.auth.name):
    if web.auth.name == 'DillonB07':
        return render_template(f'files/{user}/index.html')
    else:
        return render_template(f'files/{web.auth.name}/index.html')

Here's a runtime log:

Traceback (most recent call last):
  File "main.py", line 13, in <module>
    def user(user: str = web.auth.name):
  File "/home/runner/File-Sharing/venv/lib/python3.8/site-packages/werkzeug/local.py", line 436, in __get__
    obj = instance._get_current_object()
  File "/home/runner/File-Sharing/venv/lib/python3.8/site-packages/werkzeug/local.py", line 565, in _get_current_object
    return self.__local()  # type: ignore
  File "/home/runner/File-Sharing/venv/lib/python3.8/site-packages/replit/web/__init__.py", line 14, in <lambda>
    auth = LocalProxy(lambda: ReplitAuthContext.from_headers(flask.request.headers))
  File "/home/runner/File-Sharing/venv/lib/python3.8/site-packages/werkzeug/local.py", line 436, in __get__
    obj = instance._get_current_object()
  File "/home/runner/File-Sharing/venv/lib/python3.8/site-packages/werkzeug/local.py", line 565, in _get_current_object
    return self.__local()  # type: ignore
  File "/home/runner/File-Sharing/venv/lib/python3.8/site-packages/flask/globals.py", line 33, in _lookup_req_object
    raise RuntimeError(_request_ctx_err_msg)
RuntimeError: Working outside of request context.

This typically means that you attempted to use functionality that needed
an active HTTP request.  Consult the documentation on testing for
information about how to avoid this problem.

As I understand it, the issue comes from using web.auth.name as a default for a parameter in my view. Is this a bug, or am I just not allowed to do that?

Rest of the code: https://gist.github.com/DillonB07/dbb95c4908499d9ba34f3ef1aca6778b

Replit DB Floating Point Issue

Hey @Scoder12, we got a bug report that retrieving floating point values from replit db is broken in the latest version (v3) of this package. They say this worked in v2. Are you able to take a look?

Version 3 of the replit package throws an error when attempting to retrieve floats from the replit db

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.