os.getenv() is not pulling a variable I previously declare - python

I'm new to python and I cannot seem to figure out why I cannot get the os.getenv() function to work. It keeps returning None which is the default value if something is not there. Here is my code:
import os
import datetime
import plaid
from flask import Flask
from flask import render_template
from flask import request
from flask import jsonify
app = Flask(__name__)
PLAID_CLIENT_ID='entercharhere'
PLAID_ENV='sandbox'
#edit here
os.environ['PLAID_CLIENT_ID']='entercharhere'
os.environ['PLAID_ENV']='sandbox'
print(PLAID_CLIENT_ID)
# Fill in your Plaid API keys - https://dashboard.plaid.com/account/keys
PLAID_CLIENT_ID = os.getenv('PLAID_CLIENT_ID')
# Use 'sandbox' to test with Plaid's Sandbox environment (username: user_good,
# password: pass_good)
# Use `development` to test with live users and credentials and `production`
# to go live
PLAID_ENV = os.getenv('PLAID_ENV','sandbox')
print(PLAID_CLIENT_ID)
The output I am receiving is entercharhere and None
EDIT
I wound up switching my declaration of variables to send them to the environment using this:
os.environ['PLAID_CLIENT_ID']='entercharhere'
os.environ['PLAID_ENV']='sandbox'
Is this the best way to do it?

Works perfectly:
import os
os.environ['SOMEVAR'] = 'bla'
print(os.getenv('SOMEVAR'))
# in your shell: export SOMEVAR1="blabla"
print(os.getenv('SOMEVAR1'))
Gives:
bla
blabla

Related

How to connect i18n to flask application?

I want to make my API to support multiple languages, so it can get the lang header and respond in the same language, but I struggle when it comes part to implement this i18n.
Here's my folder structure in the left, and the translations:
My translations are in JSON format and located in /lang folder.
Here's my app.py:
# Load env
from dotenv import load_dotenv
load_dotenv()
import i18n
i18n.set('file_format', 'json')
i18n.load_path.append('/lang')
# Import init_credentials
from credentials.init_credentials import *
from flask import Flask
from flask_restful import Api
#Imports for the API
from src.Root.rootPoint import Root
#etc..
https://github.com/danhper/python-i18n#readme
It does not work when I try to use the approach from documentation.
i18n.t('phoneAlreadyLinked') #phoneAlreadyLinked

How to enable/configure Jinja line statements in Flask 1.0.4?

The Jinja documentation states the following regarding line statements:
If line statements are enabled by the application, it’s possible to mark a line as a statement.
In this video, line statements are enabled/configured like this:
from flask import Flask
app = Flask(__name__)
app.jinja_env.line_statement_prefix = '%'
However, in Flask 1.0.4 my application object does not have this attribute.
How can I enable and configure line statements?
So according to the source, app.jinja_env is a locked_cached_property which is created the first time it is accessed. So we can't set options directly on app.jinja_env.
What we can do is set app.jinja_options when we are creating our app so that when jinja goes to load the environment it looks at the default app.jinja_options in Flask already which are
jinja_options = {"extensions": ["jinja2.ext.autoescape", "jinja2.ext.with_"]}
So with that, I believe the following should do what we need
from flask import Flask
Flask.jinja_options = {'extensions': ['jinja2.ext.autoescape', 'jinja2.ext.with_'], 'line_statement_prefix': '%'}
app = Flask(__name__)
Flask breaks up the options object, passes that to the Environment which is a subclass of Jinja Environment which then assigns the line_statement_prefix.

Parse result into JSON using Python and Flask

Now, I managed to successfully pull basic information from my smart device onto the terminal using pyHS100 on python (v3.6) using the following code
from pyHS100 import SmartPlug
from pprint import pformat as pf
plug = SmartPlug("10.xxx.xxx.xxx")
print("Hardware: %s" % pf(plug.hw_info))
which results in the following:
but I can't parse the data into json format and display it on the local server for my RESTful API purpose if I done it this way:
from flask import Flask, jsonify
from flask_restful import Resource, Api
from pyHS100 import SmartPlug
app = Flask(__name__)
#app.route('/api')
def get():
plug = SmartPlug("10.xxx.xxx.xxx")
sys = plug.hw_info
return jsonify({'data':sys})
if __name__ == '__main__':
app.run(host='0.0.0.0')
app.run(debug=True)
All I need is for the information to be presented into something like this:
What did I do wrong and how do I this fix? Thanks
I believe the best way to solving this is by using json.dumps

How to stop Atom automatically putting imports to the top in python

I have this code in atom;
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
app = Flask(__name__)
app.config["SECRET_KEY"] = "D02C6E9F82CB9F4D"
app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:///site.db"
db = SQLAlchemy(app)
from flaskSite import routes
When I save this in atom it puts the from flaskSite import routes to the top but I need at the bottom.
Why does it do this?
These are the packages installed to atom;
atom-live-server
autoclave-html
autocomplete-python
emmet
file-icons
kite
script
python-autopep8
Thank you
It's for sure the autopep feature, got the same problem and have to disable that package each time I don't want to rearrange the imports.

Cant fetch production db results using Google app engine remote_api

Hey, I'm trying to work out with /remote_api with a django-patch app engine app i got running.
i want to select a few rows from my online production app locally.
i cant seem to manage to do so, everything authenticates fine, it doesnt breaks on imports, but when i try to fetch something it just doesnt print anything.
Placed the test python inside my local app dir.
#!/usr/bin/env python
#
import os
import sys
# Hardwire in appengine modules to PYTHONPATH
# or use wrapper to do it more elegantly
appengine_dirs = ['myworkingpath']
sys.path.extend(appengine_dirs)
# Add your models to path
my_root_dir = os.path.abspath(os.path.dirname(__file__))
sys.path.insert(0, my_root_dir)
from google.appengine.ext import db
from google.appengine.ext.remote_api import remote_api_stub
import getpass
APP_NAME = 'Myappname'
os.environ['AUTH_DOMAIN'] = 'gmail.com'
os.environ['USER_EMAIL'] = 'myuser#gmail.com'
def auth_func():
return (raw_input('Username:'), getpass.getpass('Password:'))
# Use local dev server by passing in as parameter:
# servername='localhost:8080'
# Otherwise, remote_api assumes you are targeting APP_NAME.appspot.com
remote_api_stub.ConfigureRemoteDatastore(APP_NAME,
'/remote_api', auth_func)
# Do stuff like your code was running on App Engine
from channel.models import Channel, Channel2Operator
myresults = mymodel.all().fetch(10)
for result in myresults:
print result.key()
it doesn't give any error or print anything. so does the remote_api console example google got. when i print the myresults i get [].
App Engine patch monkeypatches the ext.db module, mutilating the kind names. You need to make sure you import App Engine patch from your script, to give it the opportunity to mangle things as per usual, or you won't see any data returned.

Categories