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 troubleshoot local host connection

Hey all,

I have a little flask project where I am trying to have a login page. The problem I am running into right now is that the local server starts, shows the login page (http://127.0.0.1:5000/) but the page never loads. Does anyone have any suggestions on where to begin to troubleshoot this? I'm not even running any html templates. Aside from cutting out chunks of code I'm sort of stumped.


/r/flask
https://redd.it/5jx41i
Question: I'm looking for a rating/ranking system where I can assign weighted scores to multiple attributes of of items.

If this isn't a good place to ask this please ignore/remove.

I'm looking for a system that I can use to rate or rank (I'm not sure if there's a difference) a small set (10-20) items with a few attributes each based on my own arbitrary value for each attribute.

Example: I'm looking to buy a house, and I have a list of ten or so possible candidates. The attributes I will be using to compare will be: price, lot size, floor size, availability of specific high speed internet provider. The importance of each attribute is not equal, nor is the difference equal.

Special features, lots size: The bigger the lot, the better the score, but I also want the lot to be bigger than 10000sqft, so anything below that should get a penalty.

I think I'm ok enough to write the python myself, but I don't have the math to figure this out.

I would guess that something like this must already exists/be defined, but I don't know enough to ask google the right questions.

/r/pystats
https://redd.it/47z8ev
[AF] app.app_context() in thread throws "Working outside of application context from"

Hello, here is my code (simplified):

def __process(app):
with app.app_context(): # <- exception is right here
# work with app..
# work with db..

def process(app):
thread = threading.Thread(target=__process, args=(app))
thread.daemon = True
thread.start()

file with controller:

from flask import current_app

@page.route("bla")
def foo():
process(current_app)

main file:

def run():
app = Flask(__name__, static_folder=None)

setup_database(app)
register_blueprints(app)

app.run()

I've tried putting `with app.app_context()` everywhere, but it does not help - when i try to call the one in `__processs` function - it throws "RuntimeError: Working outside of application context".

Note, that i intentionally do not have access to `app` from controller, so have to use `current_app`, but it works in all other cases.

What is wrong with threaded function?

ps: i am aware about RQ/Celery and going to use something later, but want to sort this out first.

/r/flask
https://redd.it/5jrrsu
Using Python 3.6 in Jupyter in Ubuntu 16.04

# Warning: Following these instructions will set the default Python3 kernel in Jupyter to Python3.6 instead of Python3.5.
## Additional Warning: You will need to install a new copy of any previously installed library to use it in Python3.6.

Using these instructions you will create a virtual environment that contains a copy of Python3.6 so that your system Python will not be effected.

Modified from source at http://stackoverflow.com/questions/1534210/use-different-python-version-with-virtualenv

I needed to install these packages first
sudo apt install libssl-dev
sudo apt install zlib1g-dev

mkdir ~/src
wget http://www.python.org/ftp/python/3.6.0/Python-3.6.0.tgz
tar -zxvf Python-3.6.0.tgz
cd Python-3.6.0
mkdir ~/.localpython
./configure --prefix=$HOME/.localpython
make
make install

## Making the virtual environment

cd ~/src
wget https://pypi.python.org/packages/d4/0c/9840c08189e030873387a73b90ada981885010dd9aea134d6de30cd24cb8/virtualenv-15.1.0.tar.gz#md5=44e19f4134906fe2d75124427dc9b716
tar -zxvf virtualenv-15.1.0.tar.gz
cd virtualenv-15.1.0/
~/.localpython/bin/python3.6 setup.py install
virtualenv ve -p $HOME/.localpython/bin/python3.6
source ve/bin/activate

## Installing Jupyter
pip3.6 install jupyter
pip3.6 install ipython[all] # This may not be needed

To add Python3.6 to Jupyter, while in the virtual environment run the following commands:
## Warning: this will set the Python3 kernel to Python3.6
python3.6 -m pip install ipykernel
python3.6 -m ipykernel install --user

Use "deactivate" when done.

After this the virtual environment does not need to be active to use Python3.6 in Jupyter

## Final Note:
This worked on my machine, but I've installed Jupyter using pip so if you did not install this way you may have to do additional work. This also means I installed all of my libraries using pip as well.

/r/IPython
https://redd.it/5jzsi2
Simple plot - not sure what's up

I'm not sure why I'm not seeing a parabola for this simple plt -

from __future__ import division
import numpy as np
import matplotlib.pyplot as plt

# import pandas as pd
# import sympy

def f(x):
return x**x - 4*x + 7

x = np.linspace(-5, 5, 100)
# y = x**x
y = f(x)

plt.plot(
x,
y,
color="green",
marker="o",
linestyle = "solid"
)

plt.grid()

plt.show()



[Here's what mine looks like](http://i.imgur.com/nNhVwZg.png)


I'm expecting something along these lines - https://www.desmos.com/calculator/lvc9vjs91n

If anyone has any other tips / suggestions I'm all ears as I've only just started messing about with this.

Thanks


/r/pystats
https://redd.it/46qu8z
Open Science: Reply to 'Influence of cosmic ray variability on the monsoon rainfall and temperature': a false-positive in the field of solar-terrestrial research
http://nbviewer.jupyter.org/github/benlaken/Comment_BadruddinAslam2014/blob/master/Monsoon_analysis.ipynb

/r/JupyterNotebooks
https://redd.it/48xs61
What’s new in matplotlib — Matplotlib 2.0.0rc2 documentation
http://matplotlib.org/2.0.0rc2/users/whats_new.html

/r/Python
https://redd.it/5k0hfo
tablib is one of the most essential and useful python libraries

I recently used [tablib](http://docs.python-tablib.org/), the tabular datasets library for python and found that it makes lots of repetitive tasks in my usual projects (like exporting some data to `CSV`) a whole lot simpler. If you want to export a pickled list or dictionary to say csv, json or even an xls, all you have to do is this:

with open('filename.csv', 'wb') as f:
f.write(data.csv.encode('UTF-8'))

with open('filename.json', 'wb') as f:
f.write(data.json.encode('UTF-8'))

with open('filename.yaml', 'wb') as f:
f.write(data.yaml.encode('UTF-8'))

with open('filename.xls', 'wb') as f:
f.write(data.xls)

where data is a tablib dataset initialized like this:

data = tablib.Dataset()

and appended like this:

data.headers = ['col-1', 'col-2', 'col-3']
data.append(my_list)


Hope you find this pythonic library useful.

Merry Christmas to you all!

/r/Python
https://redd.it/5jyvzi
[Ask-Flask] Set a column value before commit using Flask-SQLAlchemy.

I'm writing a URL shortener API using Flask, and I'm stuck with something in my model.

I have a `Url` model with the following attributes and method:

class Url(db.Model):
# ...
id = db.Column(db.Integer, primary_key=True)
key = db.Column(db.String, unique=True, index=True)

def generate_key(self):
"""Generates a Base62 key for the URL based on its ID."""
self.key = encode(self.id)
# ...

I have a helper function `encode` that takes an integer, and returns a Base62-encoded string.

I want to pass an instance's `id` to generate the string, which I can then use to set the `key` attribute before the session commit. I searched, and the answers talked about using `db.session.flush()`. This sets the `id` attribute.

If I open a Python shell and do it manually it works fine:


>>> u1 = Url()
>>> db.session.add(u1)
>>> db.session.flush()
>>> u1.id
1
>>> u1.generate_key()
>>> u1.key
'a'
>>> u2 = Url()
>>> db.session.add(u2)
>>> db.session.flush()
>>> u2.id
2
>>> u2.generate_key()
>>> u2.key
'b'
>>> db.session.commit()


I want to automate these steps using some sort of event listener. For example, suppose I'm creating a new `Url` instance in a view. I want the key to be generated automatically either after I manually call `db.session.flush()` or right before a commit as I call `db.session.commit()`.

I saw the following events `after_flush`, `before_commit` and `after_insert` in the docs, but I can't seem to get it to work.

I got different errors including:

> sqlalchemy.exc.ArgumentError: Session event listen on a scoped_session requires that its creation callable is associated with the Session class.

> sqlalchemy.exc.InvalidRequestError: No such event 'after_flush' for target '<class 'app.models.Url'>'

Edit: I realised I could just do that in the view: create a new instance, flush, generate key, and then commit. But I wanted to see if there's another way of doing it. I just thought that logic should belong to the model.


/r/flask
https://redd.it/5dn2vb
How do I change the home directory?

Hi all. I am running on Windows 10. When I open the Jupyter Notebook launcher, my home directory is My Documents. I would like for Jupyter to start one level back. This works if I run iPython from the command prompt, but I would like the luxury of running Jupyter with a shortcut launcher. Is there anyway to configure this? I already checked online (http://jupyter-notebook-beginner-guide.readthedocs.io/en/latest/execute.html), and this was not helpful. If I follow these instructions, my starting path is already C:\Users\MY_NAME. I would like Jupyter to start here instead of C:\Users\MY_NAME\My_Documents.

/r/IPython
https://redd.it/4r37yd
What is wrong?

on terminal, it works good, but on ipython notebook it doesn't work.


```sumoProcess = subprocess.Popen([sumoBinary, "-c", "data/cross.sumocfg", "--tripinfo-output","tripinfo.xml", "--remote-port", str(PORT)], stdout=sys.stdout, stderr=sys.stderr)```


UnsupportedOperationTraceback (most recent call last)
<ipython-input-15-80cdeae4d531> in <module>()
----> 1 sumoProcess = subprocess.Popen([sumoBinary, "-c", "data/cross.sumocfg", "--tripinfo-output","tripinfo.xml", "--remote-port", str(PORT)], stdout=sys.stdout, stderr=sys.stderr)

/usr/lib/python2.7/subprocess.pyc in __init__(self, args, bufsize, executable, stdin, stdout, stderr, preexec_fn, close_fds, shell, cwd, env, universal_newlines, startupinfo, creationflags)
700 (p2cread, p2cwrite,
701 c2pread, c2pwrite,
--> 702 errread, errwrite), to_close = self._get_handles(stdin, stdout, stderr)
703
704 try:

/usr/lib/python2.7/subprocess.pyc in _get_handles(self, stdin, stdout, stderr)
1126 else:
1127 # Assuming file-like object
-> 1128 c2pwrite = stdout.fileno()
1129
1130 if stderr is None:

/usr/local/lib/python2.7/dist-packages/ipykernel/iostream.pyc in fileno(self)
304
305 def fileno(self):
--> 306 raise UnsupportedOperation("IOStream has no fileno.")
307
308 def write(self, string):

UnsupportedOperation: IOStream has no fileno.


Python 2.7.6


/r/IPython
https://redd.it/4q13m9
[AF]cannot get environment variables set in Flask application

Hi,
I tried to set up sensitive information as environment variables in CentOS, and pass them to Flask config file used in main file, i.e. __init__.py . But it did not work. The Flask application is running under Apache.

I first edit /etc/environment as root user

MAIL_USERNAME="abcde@abc.com"

then logout, login again
Then verify MAIL_USERNAME is set by running

echo $MAIL_USERNAME

This works fine

And in configuration.py, this is how I set MAIL_USERNAME.

MAIL_USERNAME = os.environ.get('MAIL_USERNAME')

for testing purpose,
I print out MAIL_USERNAME

in __init__.py
print(MAIL_USERNAME)

Then from terminal, if I run

python3.4 __init__.py

it print out correct values of MAIL_USERNAME

However, if I tested on web browser, MAIL_USERNAME is just not set. it shows NONE. I verify this by looking Apache log.

Any idea of how this works would be really appreciated.

Thanks



/r/flask
https://redd.it/5dc4dn
Django and Celery on seperate servers without code on both

So far what I've read up the code needs to live on both the servers for Celery to run the task.

My question is, is it possible for me to run a Celery instance on a separate server (more resources for running high resource tasks), without having access to the code?

/r/django
https://redd.it/5jqqkq
Seemingly random Jupyter freezes

Very often once cell will get stuck with [*] and output nothing. I can't fix that unless I relaunch Jupyter each time, wich is atrocious.

The only thing that looks weird (I'm a noob with this, though) in the console is:

[W 05:01:23.008 NotebookApp] Saving untrusted notebook ipywidgets-master/docs/source/examples/Widget List.ipynb

Sometimes it's with "big" cells, and sometimes it's with a couple of lines, usually when making quick changes.

But when it's stuck, it doesn't matter if I erase the cell, create new ones, use others... the whole thing is stuck. Only resetting kernel or relaunching the app will unstuck it.

How can I fix this little nightmare? Jupyter, you looked so cool...

Edit with more info
===
Python 2.7.11

Jupyter 4.1.0

I was using particularly small snippets from the ipywidgets library, from their own examples. Right now I can't remember the exact code and I can't seem to replicate the freeze, but it froze also using regular print statements. If it happens again I'll update this thread with the exact code.

/r/IPython
https://redd.it/4penv7
Wondering how to create a variable list of strings that saves as json in the db

I'm new to django, and for the project I'm working on I need a list of strings that gets stored to the model.

Since those string are unique to each "post" it doesn't make much sense to store them in their own table, adding to the db lookup time.

So I was wondering how I can create a list of strings that when saved, get parsed to json, and stored as a single charfield to the db?

/r/django
https://redd.it/5jr6mt