Python Daily
2.57K subscribers
1.48K photos
53 videos
2 files
38.9K links
Daily Python News
Question, Tips and Tricks, Best Practices on Python Programming Language
Find more reddit channels over at @r_channels
Download Telegram
[AF] troubleshooting flask app on heroku

Hi all,

I'm a newbie to web development and recently my friend and I have been trying to create a webapp that shows the count of various tweets using the TwitterAPI. We were able to create a working prototype that runs on my local machine, using flask integrated with socket\-io. Basically the point of the site is to update the count of tweets live. We tried to deploy this to Heroku but we faced some issues that we didn't exactly understand.

Basically our code has a separate script that runs the TwitterAPI stuff and the counts for various cases are saved into a SQLite database. From there, the main.py for the flask app would read the information every second and would update the site accordingly. However, when we tried to deploy this to Heroku, the database started to get overwritten: we figured out that when we used gunicorn, each HTTP request basically created a new thread and would basically run the database\-writing script multiple times, which caused problems. Currently, we don't know how to run this script once, independent from all the HTTP requests. We tried modifying the Procfile and tried different dynos but we then encountered some other bugs. Any help would be much appreciated!!

Thanks!

/r/flask
https://redd.it/8n50ot
How To Write Tests For Python

[https://able.bio/SamDev14/how\-to\-write\-tests\-for\-python\-\-22m3q1n](https://able.bio/SamDev14/how-to-write-tests-for-python--22m3q1n)

/r/Python
https://redd.it/8n7uay
How to exclude database password when pushing to github?

Hiho, today I setup a Djangoproject, and for the first time I`m using PostgreSQL instead of SQLite. Everything is great and setting it up, unexpectedly, was no problem.
But now I would like to know if there is a way to exclude the databasepassword from the settings.py file when I push the project to github. I know how to exclude the whole settings.py file with .gitignore, but thats not really what I would like to do.

Thats the part for the database.

DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'django',
'USER': 'me',
'PASSWORD': 'passwort',
'HOST': 'localhost',
'PORT': '',
}
}


/r/django
https://redd.it/8n8oj0
Using python manage.py shell effectively

When using django shell, i have to enter all the imports manually, and also rewrite the views for testing. This is so much of work and also a pain in a\*\*. Is their any way where i have to type less and test more. Like any tip on how to use django shell effectively.

/r/django
https://redd.it/8n8x3w
Alternative for php's include/require function in Django

[SOLVED]
Hi, I am new to django, I used to use php but now I have to work on a quite big project I have decided to use django. In php I used to reuse code using include function. eg. if there is a navbar that needs to be displayed on top of every page in the website, instead of writing html for navbar on every page I used to write it in a seperate file called `navbar.php` and the use

include('/navbar.php');

on appropriate places in every webpage.
How do I implement this in django. I looked into creating a base.html and extending it but its not quite the same.

/r/django
https://redd.it/8n6mob
Need Help With VCS/Deploying (details in description)

I've written a REST API with Django Rest Framework. I want to keep the project on a private github repo. However, I've heard that it is a bad idea to keep settings.py in version control. I've overidden the secret variables with a local_settings.py file that will be used to hold production config variables that is gitignored. However, I'm not sure how I push local_settings.py to Heroku when deploying with Heroku Git, but keep local_settings.py out of the private github repo. Furthermore, how should I manage database setup (using sqlite3 in production for now)?


I am also using Gunicorn if this is relevant.

/r/django
https://redd.it/8nf7z7
Dear Python, where have you been all my life?

I am NOT a developer. But, I've tinkered with programming (in BASIC, Visual Basic, Perl, now Python) when needed over the years, starting in my teens during the 80's and throughout my career, and had at least entry level courses in COBOL(in about 1990), C, and Java (in about 2009) while getting my degree. (I was somewhat of a nontraditional student.)

Today I decided that I needed to script something, and hoped that *learning* how to do it in Python was going to take me significantly less time than doing it manually - with the benefit of future timesavings.

No, I didn't go from 0 to production in a day. But if my coworkers will leave me alone, I *might* be in production by the end of the day tomorrow.

The last thing I used to scratch an itch like this was Perl in about 2013. Like with Python, I was learning as I went. And in that case - while some parts weren't bad, other parts were like running barefoot over sharp glass. I have working Perl from back then that I still don't *quite* understand bits of, because the best I could do is take someone else's example and massage it for my needs in some places. (In fairness, what I was doing there was a bit more complex, but it still wasn't rocket science.)

What I'm working on today isn't super complex - I'm taking some user input, making some decisions based on that input, reading a .csv, and will ultimately output a json file - but I've been able to easily understand everything I've done so far. I've done a bit of googling around the json and csv bits, and that seems pretty easy too. But putting together what I've done so far has just been a complete joy.

The syntax is so wonderful - it took me a couple of minutes to realize, for instance, that there was no "wend" or similar for a "while" loop, that merely the end of a code block would serve the same purpose. But when I did I immediately thought "wow, of course not - how efficient is that?" I feel like that's a big part of what I'm enjoying - things that maybe should be able to be implied rather than explicitly typed in or stated, actually *are* implied in many cases. It feels like there's a lot less in the way of required punctuation, that's for sure.

Overall it feels natural, intuitive, and relatively easy to understand and write the code for the basic things I'm doing - I haven't had this much fun doing stuff with code since the days fooling around with BASIC in my teens.

Well, thanks if you actually read this far. I've just found this to be so fun and exciting I had to share it with folks who might care. I think I'm going to be looking for excuses to script things with Python going forward. :-)





/r/Python
https://redd.it/8ndhel
I made a FTP/SFTP client using python and tkinter

/r/Python
https://redd.it/8niot2
ldap authenticator for jupyterhub

Hello all!

Long time lurker, first time poster.

I recently created a new ldap authenticator for jupyterhub geared more towards enterprise ldap integration. It includes support for the following features:

- Multiple LDAP servers
- Nested groups
- User home directory creation at login

Any feedback would be much appreciated. Please let me know what you think!

github: [jupyterhub-ldap-authenticator](https://github.com/hansohn/jupyterhub-ldap-authenticator)
pypi: [jupyterhub-ldap-authenticator](https://pypi.org/project/jupyterhub-ldap-authenticator/)

/r/IPython
https://redd.it/8nlh3e
Sentry is pretty popular with Django because it's mentioned in the django docs, so I wanted to ask how you're all dealing with GDPR?

I'm a bit confused on what I need to do to be able to use sentry with Django so I don't get in trouble with ico because of gdpr.


They wrote a blog post here about: https://blog.sentry.io/2018/03/14/gdpr-sentry-and-you


But I'm still kinda confused after reading it.


And GDPR is supposed to be OPT IN instead of OPT OUT, right? But if you've already given your users cookies and sent their data to sentry the first moment they connect to your site, regardless if you have a banner on the top of the screen that says "You have to agree to use cookies if you want to use this website" but they haven't agreed to anything and they received those cookies before the banner showed up.


And I see that ICO is constantly handing out huge fees nonstop to companies all day long, they just don't run out of companies to fine. They really hit jackpot with this new law. You can see every company they've taken action against on ico's website.

/r/djangolearning
https://redd.it/8nm2oz
So hypothetically speaking, what should I do to learn python as much as possible before a job interview this Tuesday

Hypothetically speaking ofc

I have no previous experience with python, only SPSS, Excel and some R knowledge. No high level of python is needed, just the basics and maybe some more.

/r/Python
https://redd.it/8nno08
Moving from Java to Python?

So I'm a high school student with experience in Java up through the AP Computer Science level, and I want to learn Python in order to work with grad students at my local university. Where should I start?

/r/Python
https://redd.it/8no5bd
Getting data from a website?

Hi, currently I have some code using BeautifulSoup4 and TKinter to have a gui where it gets the HTML of a webpage I type in the GUI, but the problem is that the webpage I need has data loaded through javascript so BS4 is not getting that data. How would I be able to get that data? I've heard of selenium and PyQt4, but these both seem kind of difficult to install.

I need this program to work on a mac and windows, currently it works on a mac where I am developing it but would like to be able to move it to the windows computer when I am done and have an easy install. Please let me know what the best thing to do in my scenario is.

/r/Python
https://redd.it/8nnqj6
GUI Library help

Okay so I decided to make a browser as my next project but Ive heard that Tkinter isnt a good option and thats the GUI Library I am familiar with.. I was thinking something like KIVY I heard its good for a lot of things, but I have no idea how to do this at all to be honest

/r/Python
https://redd.it/8npgac
In Memory cacheing or not with Django

Hiya All

Been a long time lurker and would love and would love some info on people good at web technologies.

So use case. I'm lead developer on a BI team and for some quirk of history I'm rewriting a call center application for a global company hosted on AWS. Atm the app is a monolith of a frontend and DB backend, its legacy code built by someone for whom it was their first "programming project" so it's all spaghetti code and completely useless to try and refactor . but our operators are in Asia and America. I'm rebuilding the application to be a seperated backend and frontend, the backend will be written again in Django and atm I've built it as a REST API that can handle request for data. End users will use a front end to request sets of data to do with leads and property listings and then call up those leads.

The way im currently conceptualising this is we have our database backend and a REST API in django sitting in europe, then our end users working round the world make a request to it at the start of the session, get all of their leads and listings through the API then they're held in memory for the users session and they just post individual requests that we can deal with asynchronously \(celery \+ rabbitmq most likely\).

My question is, would it be better to go for an approach where an end user logs in, and receives all the listings and all the leads at the start of the session and holds them in memory then posts to the API with single entries, or is it better to make lots of individual requests to the backend based on user interaction \(I'd imagine i'd implement some sort of drop down menu in react\) which saves on a 5\-10 second load time \(just on my local machine, for someone in mexico requesting a server in europe load times would be longer\) . And if we are to hold this stuff in memory for an end user, what would be the normal approach for this?

/r/django
https://redd.it/8nko3j
Advice on making a better form submit button? (success_url)

With the django class based FormView, I'm not sure the simplest way to use the success_url.


I mean, I don't want the user to go to another "thank you" page.


I would like something else like for example, that the form vanishes and the thank you message takes it's place.


Or, a modal appears on top of it with the tank you message.


Or just a div gets displayed with a message beneath the form "your message is sent, thanks!".


Just some ideas, but I'm not sure how to accomplish this, I could most likely figure it out.. but I like learning shortcuts and I'm sure other people would like to know this as well. Good info to note down for later use, right?


I'm not using any SPA right now, I intent to learn to use react or vue later though but right now it's not in my toolset.


I know how to do everthing I said without Django btw.. it's with Django that I'm not sure how to go about this.

/r/djangolearning
https://redd.it/8nobv5
HELP - How to use application context inside the blueprint module model which reflects exsiting db

Hi,
I'm building a flask application. Here is the structure:

root_folder
|config.py
|manage.py
|app
|__init__.py
|mod_a
|mod_b
|__init__.py
|controller.py
|model.py
|view.py


in manage.py:

from app import create_app, db
from flask_script import Manager

app = create_app('production')
app.app_context().push()
manager = Manager(app)

@app.shell_context_processor
def make_shell_context():
return {
'db': db,
'User': app.auth.models_user.User,
}

if __name__ == '__main__':
manager.run()

in __init__.py:

bootstrap = Bootstrap()
db = SQLAlchemy()
migrate = Migrate()
bcrypt = Bcrypt()
authtoken = HTTPTokenAuth(scheme='Token')
api = Api()

def create_app(config_name):
app = Flask(__name__)
app.config.from_object(config[config_name])
/////regist all the blueprint
db.init_app(app)
migrate.init_app(app, db)
bootstrap.init_app(app)
bcrypt.init_app(app)
api.init_app(app)
return app


Now I started to write code in models.py in mod_b, it needs to reflect existing database, I'm doing this:

from app import db
from sqlalchemy import MetaData

meta_db1 = MetaData()
meta_db1.reflect(db.get_engine(bind='my_db1'))
meta_db2 = MetaData()
meta_db2.reflect(db.get_engine(bind='my_db2'))

class Table1(db.Model):
__table__ = meta_db2.tables['table1']
__bind_key__ = 'my_db2'

def __repr__(self):
return self.__name__

class Table2(db.Model):
__table__ = meta_db1.tables['table2']
__bind_key__ = 'my_db1'

def __repr__(self):
return self.__name__
It always complains that Working outside of application context. Tried to import app, but in app there is no app_context()
What should I do to make sure models & reflection will have the correct application context? Thanks a lot for your help

/r/flask
https://redd.it/8nqsra
How to Prefetch_related with Formsets?

I'm getting too many duplicated queries in a UpdateView, one from each Formset instances and I don't know how to apply *prefetch\_related* with this case.

I've three models: Entry, that has multiple instances of Boxes, and each one has a FK to Material. So, in one Entry the user has multiples Boxes and each one has one Material associated. The problem is, when the user edits the Entry, for every Box he has, Django hits the DB looking for the material. How can I implement *prefetch\_related* \(or maybe *select\_related*?\) to solve this?

This is my code:

**models.py**

class Entry(models.Model):
....
# No Foreign keys here

class Box(models.Model):
....
material = models.ForeignKey('Material', on_delete=models.CASCADE)

class Material(models.Model:
....
# No Foreign keys here

**views.py**

class EntryUpdateView(UpdateView):
model = Entry
form_class = EntryForm

def get_context_data(self, **kwargs):
data = super(EntryUpdateView, self).get_context_data(**kwargs)
data['materials'] = Material.objects.all()
return data

def get(self, request, *args, **kwargs):
self.object = self.get_object()
form_class = self.get_form_class()
form = self.get_form(form_class)
box_form = BoxUpdateFormSet(instance=self.object)

return self.render_to_response(self.get_context_data(form=form, box_form=box_form))

**models.py**

class EntryForm(ModelForm):
class Meta:
model = Entry
fields = ...

class BoxForm(ModelForm):
class Meta:
model = Box
fields = ....

BoxUpdateFormSet = inlineformset_factory(Entry, Box, form=BoxForm, can_delete=True, extra=0)

/r/django
https://redd.it/8nqldy