Giter VIP home page Giter VIP logo

Comments (4)

di avatar di commented on July 28, 2024 1

Hi @mayanksha thanks for filing an issue.

I'd say this isn't a bug, but you also aren't really doing anything wrong here: you're just relying on an undefined / unofficial implementation detail. We don't explicitly document that from flask import current_app should work, and it's not officially supported.

That said, given that this works when deployed, I think this is a trivial enough change that we can make an update to the framework to maintain parity.

Can you try installing the branch from #76 locally and confirm that it works for you? You should be able to do that with:

$ pip install git+https://github.com/googlecloudplatform/functions-framework-python.git@fix/75

from functions-framework-python.

mayanksha avatar mayanksha commented on July 28, 2024

I tested these changes and I can confirm that it works as expected. Thanks! :-)

from functions-framework-python.

rez0n avatar rez0n commented on July 28, 2024

HI @mayanksha @di
I'm trying to do same using latest version of functions-framework but having another error on db = SQLAlchemy(app)

from flask import jsonify
import functions_framework

from flask import current_app as app
from flask_sqlalchemy import SQLAlchemy


def setup_context():
    app.config["SQLALCHEMY_DATABASE_URI"] = 'sqlite:///'
    app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
    db = SQLAlchemy(app)
    return db


with app.app_context():
    db = setup_context()


@functions_framework.http
def posts_test(request):
    request_json = request.get_json(silent=True)
    request_args = request.args
    return jsonify({'success': True})
Traceback (most recent call last):
  File "/Users/dv/Library/Caches/pypoetry/virtualenvs/cloudbench-U2d1CbNl-py3.9/bin/functions-framework", line 8, in <module>
    sys.exit(_cli())
  File "/Users/dv/Library/Caches/pypoetry/virtualenvs/cloudbench-U2d1CbNl-py3.9/lib/python3.9/site-packages/click/core.py", line 1130, in __call__
    return self.main(*args, **kwargs)
  File "/Users/dv/Library/Caches/pypoetry/virtualenvs/cloudbench-U2d1CbNl-py3.9/lib/python3.9/site-packages/click/core.py", line 1055, in main
    rv = self.invoke(ctx)
  File "/Users/dv/Library/Caches/pypoetry/virtualenvs/cloudbench-U2d1CbNl-py3.9/lib/python3.9/site-packages/click/core.py", line 1404, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "/Users/dv/Library/Caches/pypoetry/virtualenvs/cloudbench-U2d1CbNl-py3.9/lib/python3.9/site-packages/click/core.py", line 760, in invoke
    return __callback(*args, **kwargs)
  File "/Users/dv/Library/Caches/pypoetry/virtualenvs/cloudbench-U2d1CbNl-py3.9/lib/python3.9/site-packages/functions_framework/_cli.py", line 36, in _cli
    app = create_app(target, source, signature_type)
  File "/Users/dv/Library/Caches/pypoetry/virtualenvs/cloudbench-U2d1CbNl-py3.9/lib/python3.9/site-packages/functions_framework/__init__.py", line 353, in create_app
    spec.loader.exec_module(source_module)
  File "<frozen importlib._bootstrap_external>", line 850, in exec_module
  File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
  File "/Users/dv/Library/CloudStorage/Dropbox/cloudbench/gcloud-functions-python-postgres/main.py", line 39, in <module>
    db = setup_context()
  File "/Users/dv/Library/CloudStorage/Dropbox/cloudbench/gcloud-functions-python-postgres/main.py", line 34, in setup_context
    db = SQLAlchemy(app)
  File "/Users/dv/Library/Caches/pypoetry/virtualenvs/cloudbench-U2d1CbNl-py3.9/lib/python3.9/site-packages/flask_sqlalchemy/extension.py", line 219, in __init__
    self.init_app(app)
  File "/Users/dv/Library/Caches/pypoetry/virtualenvs/cloudbench-U2d1CbNl-py3.9/lib/python3.9/site-packages/flask_sqlalchemy/extension.py", line 311, in init_app
    engines = self._app_engines.setdefault(app, {})
  File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.9/lib/python3.9/weakref.py", line 498, in setdefault
    return self.data.setdefault(ref(key, self._remove),default)
TypeError: cannot create weak reference to 'LocalProxy' object

Please share a solution if you have.

from functions-framework-python.

vincerubinetti avatar vincerubinetti commented on July 28, 2024

I was trying to include flask-compress to gzip all my responses, which needs access to the Flask app. Doing this seems to work:

from flask import current_app as app
from flask_compress import Compress

Compress(app)

from functions-framework-python.

Related Issues (20)

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.