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
# 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
Stack Overflow
Use different Python version with virtualenv
How do I create a virtual environment for a specified version of Python?
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
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
http://nbviewer.jupyter.org/github/benlaken/Comment_BadruddinAslam2014/blob/master/Monsoon_analysis.ipynb
/r/JupyterNotebooks
https://redd.it/48xs61
nbviewer.jupyter.org
Notebook on nbviewer
Check out this Jupyter notebook!
[ANN] IPython 5.0 is out!
https://mail.scipy.org/pipermail/ipython-dev/2016-July/017175.html
/r/IPython
https://redd.it/4ruhol
https://mail.scipy.org/pipermail/ipython-dev/2016-July/017175.html
/r/IPython
https://redd.it/4ruhol
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
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
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
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
reddit
[Ask-Flask] Set a column value before commit using... • /r/flask
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...
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
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
[D] Deep Learning Race: A Survey of Industry Players’ Strategies – Intuition Machine
https://medium.com/intuitionmachine/the-different-ways-that-internet-giants-approach-deep-learning-research-753c9f99d9f1#.axn5kt1ny
/r/MachineLearning
https://redd.it/5jyg0e
https://medium.com/intuitionmachine/the-different-ways-that-internet-giants-approach-deep-learning-research-753c9f99d9f1#.axn5kt1ny
/r/MachineLearning
https://redd.it/5jyg0e
Medium
Deep Learning Race: A Survey of Industry Players’ Strategies
I’ve been working for quite a while now in trying to make sense of the research developments in Deep Learning. The methodology I’ve…
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
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
reddit
What is wrong? • /r/IPython
on terminal, it works good, but on ipython notebook it doesn't work. ```sumoProcess = subprocess.Popen([sumoBinary, "-c", "data/cross.sumocfg",...
[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
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
reddit
[AF]cannot get environment variables set in Flask... • /r/flask
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. ...
Python Psychologist — Repeated Measures ANOVA using Python
http://pythonpsychologist.tumblr.com/post/139246503057/repeated-measures-anova-using-python
/r/pystats
https://redd.it/468swd
http://pythonpsychologist.tumblr.com/post/139246503057/repeated-measures-anova-using-python
/r/pystats
https://redd.it/468swd
Python Psychologist
Repeated Measures ANOVA using Python
In this post I am going to show an example how to calculate a repeated measures ANOVA using Python. As far as I know there are no packages for Python doing a repeated measures ANOVA (you can do it...
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
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
reddit
Django and Celery on seperate servers without code on both • /r/django
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...
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
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
reddit
Seemingly random Jupyter freezes • /r/IPython
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...
socketserver: the Python networking module you didn’t know you needed
http://silverwingedseraph.net/socketserver-the-python-networking-module-you-didnt-know-you-needed
/r/Python
https://redd.it/5k1ie7
http://silverwingedseraph.net/socketserver-the-python-networking-module-you-didnt-know-you-needed
/r/Python
https://redd.it/5k1ie7
reddit
socketserver: the Python networking module you didn’t... • /r/Python
33 points and 2 comments so far on reddit
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
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
reddit
Wondering how to create a variable list of strings... • /r/django
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...
Thinking of writing a tutorial. Would like some feedback.
As the title states, I'm thinking of writing a tutorial for Django. I would like to get some feedback and suggestions from the community regarding the content or if the tutorial itself is even a good idea.
The tutorial will be at an intermediate to advanced level. Though, I could write it in such a way that beginners will be able to understand it.
The idea is as follows:
## How to build a REST API in Django without a framework
The tutorial will guide a user through setting up a simple application with 3 - 5 models and make these models available through a RESTful API. Further tutorials may include how to utilize modern front end technologies to build a SPA if there is enough interest.
You may ask; "Why not use a REST framework like DRF or Tastypie?". Well, I do have an answer. They are not always the best choice for an application. In many cases, they are simply unnecessary. Here are the reasons why using a REST framework may not be right for an application:
### Bloat:
Most REST frameworks come with a lot of functionality that a given developer will not use. Some come with entire work flows that a developer will not use. Do you need all of that code that checks whether or not you are using session or JWT authentication if you're using Token authentication? Sending and receiving JSON data is a remarkably simple task. Django has a built in `JsonResponse` class that can do this for you. All you have to do is provide it a dict. The hard parts are (de)serialization, filtering, pagination, etc. Django has these facilities built in as well, but few people really know how to use them in conjunction with one another. My tutorial will aim to remedy this.
### Cognitive Load
Each REST framework has it's own idioms and best practices. It forces the developer to have to learn an entire new library. This forces the developer to pick a single framework and stick with it to become acquainted with it enough to use it effectively. A great example of this is how differently Tastypie and DRF handle permissions.
### Lack of simple flexibility
Most REST frameworks are completely customizable through overriding base classes. In DRF for instance, you can override the default serializer class to enforce your own custom behavior. The only problem is that knowing which classes to override and how requires an intimate knowledge of the framework or a healthy amount of research.
### Nested resources are a pain in the ass
DRF and Tastypie come with built in functionality to allow for the creation of nested resources. However, these never seem to work in exactly the way you want them to unless you override a class or method to make them do so. I have run into some nasty bugs with both of these frameworks when trying to create a nested resource. Django's ORM does a really great job of taking rows in your database and transforming them into simple objects. It really doesn't need to be difficult to nest them, especially when they have relations.
### Implicit non-declaritive classes
This is fine if you are tacking on an API endpoint as an afterthought. However, if the API is the core of the application, it's generally not such a great idea in my opinion. Both Tastypie and DRF allow the developer to simply provide the serializer/resource class with a model or queryset and then all the work happens behind the scenes. You can declare any fields that you want excluded and even have some options for enforcing simple validation and permissions. What if you need to enforce validation on a field to field basis? What if you need two similar fields to have very different validation methods? What if you need to validate fields based on other fields? What if you need to also perform permissions checks based on a certain field in conjunction with the requesting user? DRF and Tastypie both have answers to all of these questions, though they may not be very simple answers.
## Caveats
I am in no way trying to bash on Tastypie or DRF. They are both very useful and valuable tools if you need an all e
As the title states, I'm thinking of writing a tutorial for Django. I would like to get some feedback and suggestions from the community regarding the content or if the tutorial itself is even a good idea.
The tutorial will be at an intermediate to advanced level. Though, I could write it in such a way that beginners will be able to understand it.
The idea is as follows:
## How to build a REST API in Django without a framework
The tutorial will guide a user through setting up a simple application with 3 - 5 models and make these models available through a RESTful API. Further tutorials may include how to utilize modern front end technologies to build a SPA if there is enough interest.
You may ask; "Why not use a REST framework like DRF or Tastypie?". Well, I do have an answer. They are not always the best choice for an application. In many cases, they are simply unnecessary. Here are the reasons why using a REST framework may not be right for an application:
### Bloat:
Most REST frameworks come with a lot of functionality that a given developer will not use. Some come with entire work flows that a developer will not use. Do you need all of that code that checks whether or not you are using session or JWT authentication if you're using Token authentication? Sending and receiving JSON data is a remarkably simple task. Django has a built in `JsonResponse` class that can do this for you. All you have to do is provide it a dict. The hard parts are (de)serialization, filtering, pagination, etc. Django has these facilities built in as well, but few people really know how to use them in conjunction with one another. My tutorial will aim to remedy this.
### Cognitive Load
Each REST framework has it's own idioms and best practices. It forces the developer to have to learn an entire new library. This forces the developer to pick a single framework and stick with it to become acquainted with it enough to use it effectively. A great example of this is how differently Tastypie and DRF handle permissions.
### Lack of simple flexibility
Most REST frameworks are completely customizable through overriding base classes. In DRF for instance, you can override the default serializer class to enforce your own custom behavior. The only problem is that knowing which classes to override and how requires an intimate knowledge of the framework or a healthy amount of research.
### Nested resources are a pain in the ass
DRF and Tastypie come with built in functionality to allow for the creation of nested resources. However, these never seem to work in exactly the way you want them to unless you override a class or method to make them do so. I have run into some nasty bugs with both of these frameworks when trying to create a nested resource. Django's ORM does a really great job of taking rows in your database and transforming them into simple objects. It really doesn't need to be difficult to nest them, especially when they have relations.
### Implicit non-declaritive classes
This is fine if you are tacking on an API endpoint as an afterthought. However, if the API is the core of the application, it's generally not such a great idea in my opinion. Both Tastypie and DRF allow the developer to simply provide the serializer/resource class with a model or queryset and then all the work happens behind the scenes. You can declare any fields that you want excluded and even have some options for enforcing simple validation and permissions. What if you need to enforce validation on a field to field basis? What if you need two similar fields to have very different validation methods? What if you need to validate fields based on other fields? What if you need to also perform permissions checks based on a certain field in conjunction with the requesting user? DRF and Tastypie both have answers to all of these questions, though they may not be very simple answers.
## Caveats
I am in no way trying to bash on Tastypie or DRF. They are both very useful and valuable tools if you need an all e
ncompassing solution that uses a majority of their features. I just believe that many applications would be less complex if they chose not to use these tools. Both DRF and Tastypie do a great job and you should still use them if you like to use them. I simply want to offer an alternative tool for your tool belt.
## The Tutorial
Ok, here's the meat and potatoes:
I want to write a tutorial that walks a reader through creating a REST API using Marhsmallow schemas and two custom view classes that inherit from `django.views.generic.View`. These view classes will handle pagination, filtering, object creation, object deletion, listing objects, getting a single object, and permissions. The schema will handle data validation and (de)serialization.
The tutorial will also cover creating nested resources in the form of the following: `/api/v1/users/<id>/posts/` which returns a paginated and filterable list of a user's posts.
I know many of you in this sub already know how to do this, but the tutorial will be written for those who do not.
So, if you could help me by answering any of the following questions, that would be fantastic:
* What would you like to see in a Django REST API tutorial in general?
* Have you tried to build a REST API in Django without a framework before?
* What are some API features you have found difficult to implement in the past?
* Have you used Marshmallow before?
* What are your thoughts on the method I am presenting here?
* Do you think this tutorial would be helpful for an intermediate Django developer?
* What kind of application do you think would be best to demonstrate this concept?
* Do you think this tutorial is a good idea? Why or why not?
I'm thinking I'll probably put the tutorial in a Github repo so others can help if they so wish. I am also building another website where the tutorial will be posted. I may also put it up on medium or something like that.
If you have any questions of your own, I'll be happy to answer them.
/r/django
https://redd.it/5js4yf
## The Tutorial
Ok, here's the meat and potatoes:
I want to write a tutorial that walks a reader through creating a REST API using Marhsmallow schemas and two custom view classes that inherit from `django.views.generic.View`. These view classes will handle pagination, filtering, object creation, object deletion, listing objects, getting a single object, and permissions. The schema will handle data validation and (de)serialization.
The tutorial will also cover creating nested resources in the form of the following: `/api/v1/users/<id>/posts/` which returns a paginated and filterable list of a user's posts.
I know many of you in this sub already know how to do this, but the tutorial will be written for those who do not.
So, if you could help me by answering any of the following questions, that would be fantastic:
* What would you like to see in a Django REST API tutorial in general?
* Have you tried to build a REST API in Django without a framework before?
* What are some API features you have found difficult to implement in the past?
* Have you used Marshmallow before?
* What are your thoughts on the method I am presenting here?
* Do you think this tutorial would be helpful for an intermediate Django developer?
* What kind of application do you think would be best to demonstrate this concept?
* Do you think this tutorial is a good idea? Why or why not?
I'm thinking I'll probably put the tutorial in a Github repo so others can help if they so wish. I am also building another website where the tutorial will be posted. I may also put it up on medium or something like that.
If you have any questions of your own, I'll be happy to answer them.
/r/django
https://redd.it/5js4yf
reddit
Thinking of writing a tutorial. Would like some feedback. • /r/django
As the title states, I'm thinking of writing a tutorial for Django. I would like to get some feedback and suggestions from the community regarding...
[P] Neural Painter: Paint artistic patterns using random neural network
https://github.com/zxytim/neural-painter
/r/MachineLearning
https://redd.it/5k355f
https://github.com/zxytim/neural-painter
/r/MachineLearning
https://redd.it/5k355f
GitHub
zxytim/neural-painter
Paint artistic patterns using random neural network - zxytim/neural-painter
Pythonanywhere. All static files are working except in django admin?
What am I doing wrong here? I am trying to use the django-material library. It works locally but not on python anywhere. I have run `python manage.py collectstatic` I can't figure out how to get the server to point to the admin static files! Below is my settings.py
INSTALLED_APPS = (
'material',
'material.admin',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
......)
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
'django.template.context_processors.media',
],
},
},
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, "static")
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, "media")
/r/djangolearning
https://redd.it/5ieh7z
What am I doing wrong here? I am trying to use the django-material library. It works locally but not on python anywhere. I have run `python manage.py collectstatic` I can't figure out how to get the server to point to the admin static files! Below is my settings.py
INSTALLED_APPS = (
'material',
'material.admin',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
......)
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
'django.template.context_processors.media',
],
},
},
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, "static")
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, "media")
/r/djangolearning
https://redd.it/5ieh7z
reddit
Pythonanywhere. All static files are working... • /r/djangolearning
What am I doing wrong here? I am trying to use the django-material library. It works locally but not on python anywhere. I have run `python...