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
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
Tips on understanding Django sessions.

So I've been learning and using Django for a while now. Recently I was in need of using Django's session framework but to be honest I still don't fully understand it. And when it comes to Django's documentation I find it a bit to technical for my understanding. Is there anyone that knows a good source that explains using the session framework?

/r/djangolearning
https://redd.it/8bl3kr
How to create widgets within same row?

I'm trying to a column of different widgets but can't seem to find anything like it.

For clarity: https://imgur.com/a/7L1q0

/r/IPython
https://redd.it/8blu3z
How to undo migration after pip uninstalling 3rd party package

So I pip installed the django-axes package today. Migrated the migrations that came with it. Then it turned out that the package installed was missing the needed "backends.py" file for some reason. I copied the one on the git repo and dropped it into the installed package folder and it gave me another error.

So I pip uninstalled the whole thing. But looking into my PostgreSQL database, the two tables (axes_accessattempt and axes_accesslog) created by the django-axes migrations are still there.

How is this type of situation handled normally? Just run a SQL command to drop those 2 tables manually? I already have some data in the database, so I don't want to mess it up inadvertently further.

Also, is it normal for a pip install package to be missing vital files?

Thanks.

/r/django
https://redd.it/8bpekc
Python's Pandas in C++

I have implemented a C++ library with similar interface and functionality to Pandas at https://github.com/hosseinmoein/DataFrame. It still lacks a lot of functionalities which I will add gradually. I appreciates your constructive criticisms and suggestions.

/r/Python
https://redd.it/8bq5js
[AF] Handling nested dictionaries with standard python requests library

Hey /r/flask,

I recently got my first [Flask webapp](http://www.asosquery.com/) hosted, and built a relatively simple API around it. It's driven by a very large database full of open meteorological surface observations (more info on the `/about` page), and right now I'm able to query the API with the following sample code (a separate script written for testing):

import requests
import json

r = requests.get('http://www.asosquery.com/api/v1/stations?state=PA&lat={"gt": 39, "lt": 50}')
content = json.loads(r)

This returns JSON-encoded information regarding all [ASOS stations](http://www.nws.noaa.gov/ost/asostech.html) with a latitude greater than 39.00 N and less than 50.00 N. A SQLAlchemy query of the MySQL database is dynamically generated based on the arguments in the `GET` request.

However, I would like to be able to do something like this, following an example on the [requests package documentation](http://docs.python-requests.org/en/master/user/quickstart/#passing-parameters-in-urls):

import requests # standard python requests package
import json

payload = {
'state': 'PA',
'lat': {
'gt': 39,
'lt': 50
}
}

r = requests.get('http://www.asosquery.com/api/v1/stations', params=payload)
content = json.loads(r)

This exercise fails each time. In the Flask route `/api/v1/stations`, I've tried printing the request made to the server like this:

from flask import request

@app.route('/api/v1/stations', methods=['GET'])
def get_stations():
print(request)
print(request.args)
# -- do the query stuff here -- #
return jsonify(query_results)

But, the output (prior to a raised error) is not as I expected it to be:

# print(request) prints:
<Request 'http://localhost:5000/api/v1/stations?lat=lt&lat=gt&state=PA' [GET]>
# print(request.args) prints:
ImmutableMultiDict([('lat', 'lt'), ('lat', 'gt'), ('state', 'PA')])

I've tried encoding the `payload` as JSON with the following:

r = requests.get('http://www.asosquery.com/api/v1/stations', params=json.dumps(payload), headers={'Content-Type': 'application/json'})

but execution of that code prints the following in from the flask route:

# print(request) prints:
<Request 'http://localhost:5000/api/v1/stations?{"state": "PA", "lat": {"gt": 39, "lt": 50}}' [GET]>
# print(request.args) prints:
ImmutableMultiDict([('{"state": "PA", "lat": {"gt": 39, "lt": 50}}', '')])

Here, I don't really understand why the `requests.args` is formatted like that... The information in the `key` is pretty much what I expect it to be, but the blank `value` is throwing me for a loop. I could probably handle that by just processing the information in the `key`, but that's going to require an extra level of complexity I'd like to be able to avoid.

Anybody know a good workaround? Am I handling the greater-than, less-than functionality in the best way possible?

Thank you in advance to all who offer some insight.

/r/flask
https://redd.it/8bsh8h
setInterval not auto refreshing.

Not sure what the issue is. I am using setInterval auto refresh data. The page is not actually going to the route. It seems like it is just reloading the old data.

Flask endpoint
@mod.route('/autoloaddata/', endpoint='autoloaddata')

I do an initial load when you first visit the page. Which works.
$(function(){
$("#loaddata").load("{{ url_for('datapage.autoloaddata') }}");
});

setInterval(function()
{
$('#testreload').load("autoload/");
$('#currentmap').load("autoload/");
}, 30000);

Looking at my console window `https://example.com/datapage/autoloaddata/` is coming back. But with the original load data. Its not hitting the route again.
if I copy the link from my console it hits the route and brings back new data.


/r/flask
https://redd.it/8brms4
RPi3, Flask and ds18b20 temperature sensor.

I need help.
I would like to use my Raspberry Pi 3 to get the temperature reading from ds18b20 sensor(1-wire) and then use flask to answer to GET requests with:
{
"temperature": 25.8
}
Right now I have successfully read data in console with python, but I can't figure it out how to combine this all with flask.

Maybe anyone has done it or can help me otherwise?

/r/flask
https://redd.it/8bq6u5