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
What's everyone working on this week?

Tell /r/python what you're working on this week! You can be bragging, grousing, sharing your passion, or explaining your pain. Talk about your current project or your pet project; whatever you want to share.


/r/Python
https://redd.it/8b7uc8
Heroku Procfile automatically turns to .txt file

Hey guys i have been changing the Procfile name on Heroku with no extension through the cmd but every time i do a "git push heroku master" automatically the Procfile turns to Procfile.txt! What could have caused the problem?

/r/djangolearning
https://redd.it/8b7nz7
How do I configure Jupyter Notebooks to open in a browser?

When I open a Jupyter Notebook, I want it to open in a new tab of an existing Firefox window. In my ~/.jupyter/jupyter_notebook_config.py file, I try

c.NotebookApp.browser = '/usr/bin/firefox'

With this, I get a popup error from Firefox to the effect that "Firefox is already running", which isn't unexpected. You need to use command line flags to affect an already existing instance of Firefox, so I try

c.NotebookApp.browser = '/usr/bin/firefox -P default -new-tab'

When I do this, the terminal output from Jupyter gives the error

FileNotFoundError: [Errno 2] No such file or directory: '/usr/bin/firefox -P default -new-tab': '/usr/bin/firefox -P default -new-tab'

How do I tell Jupyter to open Firefox with command-line options?

/r/JupyterNotebooks
https://redd.it/8b8mtq
Best way to handle file uploads?

Im trying to setup image uploads in a local Flask app with Flask-uploads. I think Im almost there but not able to finish it.

This is my form (using flask_wtf):

class AddBookForm(FlaskForm):
title = StringField('Title', validators=[DataRequired()])
author = StringField('Author', validators=[DataRequired()])
category = StringField('Category', validators=[DataRequired()])
image = FileField('Image', validators=[FileRequired()])

this is my template:

<form action="/upload" method="post" enctype="multipart/form-data" >
<span class="btn btn-default btn-file">
Browse <input type="file" name="image">
</span>

<input type="submit" value="Upload your image" class="btn btn-primary">
</form>

and then finally these are the important bits from views.py:

from flask.ext.uploads import UploadSet, configure_uploads, IMAGES, UploadNotAllowed

photos = UploadSet('photos', IMAGES, default_dest=lambda app: os.path.basename('uploads'))
configure_uploads(app, (photos,))

@app.route('/upload', methods=['GET', 'POST'])
def upload():
if request.method == 'POST' and 'image' in request.files:
filename = photos.save(request.files['image'])
rec = Photo(filename=filename, user=g.user.id)
rec.store()
flash("Photo saved.")
return redirect(url_for('show', id=rec.id))
return render_template('upload.html')

@app.route('/photo/<id>')
def show(id):
photo = Photo.load(id)
if photo is None:
abort(404)
url = photos.url(photo.filename)
return render_template('show.html', url=url, photo=photo)

As it stands right now when I go to /upload and fill in the form with an image the file does actually get uploaded to my 'uploads' directory in my app but this error is shown in the console:

File "/Users/david/Documents/projects/flask_store/storeapp/views.py", line 236, in upload
rec = Photo(filename=filename, user=g.user.id)
NameError: name 'Photo' is not defined

there is no object named 'Photo' but if I changed the fields in my from from 'image' to 'photo' then try and upload an image again I get no errors that time but the image doesn't get uploaded to the local 'uploads' directory.

What am I missing here?

thanks


/r/flask
https://redd.it/8b8l7l
Flask and nodejs

Ok i have been reading online and i see that a lot of people and alot of companies are switching to nodejs im not a professional web developer but i like to build for personal use and for learning.

Is flask or django dying and what are the benefits of nodejs over flask (which is what i use)

/r/flask
https://redd.it/8b0pc9
How do you figure out whether the duck needs to quack?

I have been using Python for a while and am loving it. However, as I begin to use old code or other people's code, I have found that it is getting really difficult to determine the interface people use since they rarely explicitly state it.

In Java the interface is made explicit and you can easily look-up what methods and types you need to define in order to expand or use another piece of code. But in Python, I have been spending a painful amount of time tracing through every part of a program to try and find out what methods I need for the object I am passing to some function. And if I miss a method I may not realize it till down the road when some obscure piece of code gets hit at run-time and I find out my object didn't have all that it needed.

Am I missing something here? Is there some proper way to Python so that the object interface is obvious? Are other people just not writing good Python code?

I feel like 'duck' typing is one of the biggest assets of Python, as it lets you work with objects based on how they function, but it seems exceedingly difficult to actually figure out whether the duck is supposed to quack or not.

Note: PyCharm is okay for some of this, but really doesn't hack it for anything but the simplest programs. And should it really be left to the IDE for something so important?

/r/Python
https://redd.it/8bebmr
[AF] SQLite Database Singleton

I've been cobbling together a Flask App which uses SQLite. I was wondering if someone would be prepared to weigh in on my current method of accessing the database - using a singleton?

Basically I am creating a Database singleton in *before_first_request* which connects to the database calling *sqlite3.connect()*.

I've noticed that in the Flask docs they provide an [SQLite example](http://flask.pocoo.org/docs/0.12/patterns/sqlite3/) which stores the database against the *flask.g* object.

My main concern with my approach would be with regard to thread safety, which I presume will depend heavily on the server I intend to use in production?

This flask docs article seems to be focused on connecting to the db on demand - but if requests to my app will always need to access the database, then I would assume its better to connect only once?




/r/flask
https://redd.it/89f1be
asyncio/aiohttp question

Hello, I am new to django and python in general. My django back-end calls amazon api and returns parsed response to front-end. Now I have to make it asynchronous.

My view looks something like this (very simplified):

class AmazonSearchAPI(viewsets.ViewSet):

def get_products(self, request):
response = requests.get(request_params)
return Response(response, status=HTTP_200_OK)

Question is, do I need aiohttp? Can I just add asyncio and write "async def get_products(self, request)"?

/r/django
https://redd.it/8bfgp8
[P] Deep reinforcement Learning course: Q-learning article and DQN with Doom notebook are published

Hello, I'm currently writing a series of free articles about Deep Reinforcement Learning, where we'll learn the main algorithms (from Q* learning to PPO), and how to implement them in Tensorflow.

The second article is published, it's about Q-learning and we'll learn to **implement a Q-learning algorithm with Numpy and OpenAI Gym.**

**Q-learning article**: https://medium.freecodecamp.org/diving-deeper-into-reinforcement-learning-with-q-learning-c18d0db58efe

Moreover, the third article (Deep Q Learning with Doom) will be published **this week**, but the implementation of a Doom playing agent with Tensorflow **is already published.**

**Doom Deep Q agent**: https://github.com/simoninithomas/Deep_reinforcement_learning_Course/blob/master/DQN%20Doom/Deep%20Q%20learning%20with%20Doom.ipynb

Let me know what you think! What architectures you want and any feedback.

**The Syllabus**: https://simoninithomas.github.io/Deep_reinforcement_learning_Course/

**Introduction to Reinforcement Learning**: https://medium.freecodecamp.org/an-introduction-to-reinforcement-learning-4339519de419

Thanks!


/r/MachineLearning
https://redd.it/8beohl
Documenting a Django Rest Framework API

I'm using DRF for the first time to create a read-only API. It all works fine, but I'm trying to work out how to automatically generate some documentation for it.

I've got a Swagger UI page up and running using [django-rest-swagger](https://marcgibbons.com/django-rest-swagger/), which is OK... but some of my API endpoints have optional GET arguments and I can't see any way to document those in a nice way. I can mention them in comments on my DRF Views, but I thought there'd be a way to format them nicely, maybe have them appear as fields in the Swagger UI, like `lookup_url_kwarg`s do.

I'm going round in circles, reading the DRF [Documenting your API documentation](http://www.django-rest-framework.org/topics/documenting-your-api/), all the third-party tools they mention there, laborious workarounds like [this](https://github.com/m-haziq/django-rest-swagger-docs#advance-usage)...

Does anyone have a good, thorough example of how to nicely document a DRF API?

/r/django
https://redd.it/8b9kjo
How to properly implement a button to change state.

In a project I'm working on I'd like to have a button that would change an object's state from one to another

For example

`MyObject.approved = True`

Right now I'm using a button wrapped inside form tags with a csrf_token tag, is this the way to do it or is there another one?



/r/djangolearning
https://redd.it/8biczy
How does Jupyter find modules?

I'm trying to troubleshoot an issue where my Jupyter Notebook can't find an installed module, but documentation on this is sparse. How do I specify where any given instance of Jupyter Notebook looks for modules?

A possibly related question: When I use `jupyter --path`, I see a listing of directories under the "data" heading. Is this where Jupyter looks for modules, and if so, what file do I edit to change it?

/r/IPython
https://redd.it/8biqq7
Jupyter background data storage is confusing

For example:

> from abc import xyz as 123 # creates alias 123 for xyz

Then I delete the code above, replace it with the code below and rerun the cell.

> from abc import xyz

Nonetheless, alias 123 remains functional as it stored in memory and is only erased if you restart the kernel.

This leads to confusion (or forces you to keep track of more things) when you run the code after making edits - some errors are not raised because their dependencies (such as 123) are stored an remain accessible.

Also, all variables (even those defined within a function) are global.

My intuition is that these differences (from normal ide coding environment) will result in confusion and I want to turn of this feature; however, it might be the case that I am misusing Jupyter and these features are in fact advantageous. If so, can you please educate me?

Thank you very much

Edit: formating

/r/IPython
https://redd.it/8blldv