When I create a new IPython notebook it opens a blank notebook. I would like instead that all my notebooks open with a few cells already populated with stuff that I always use. For example, the first cell would have some magic commands
%load_ext autoreload
%autoreload 2
%matplotlib inline
The second cell might contain some standard imports
import numpy as np
import matplotlib as mpl
from matplotlib import pyplot as plt
from matplotlib.collections import PatchCollection
from netCDF4 import Dataset
from tabulate import tabulate
Is it possible to tell IPython somewhere how to template new files so that this can be done?
You could set up a configuration file for ipython as described here. For example run:
ipython profile create
to create a default ipython profile (probably with the name ${HOME}/.ipython/profile_default/ipython_config.py directory) (or you could just create this file yourself). You could then edit this to include e.g.:
c = get_config()
c.InteractiveShellApp.exec_lines = [
'%load_ext autoreload',
'%autoreloud 2',
'%matplotlib inline',
'import numpy as np',
'import matplotlib as mpl',
'from matplotlib import pyplot as plt',
'from matplotlib.collections import PatchCollection',
'from netCDF4 import Dataset',
'from tabulate import tabulate'
]
However, this just runs commands in the background before your notebook ipython session starts and doesn't actually populate the first cell.
Related
I am trying to import Image from PILLOW.
from PILLOW import Image
im = Image.open("bride.jpg")
im.rotate(45).show()
Undefined function 'from' for input arguments of type 'char'.
>> import PIL.Image
im = Image.open("bride.jpg")
im.rotate(45).show()
Error using import
Import argument 'PIL.Image' cannot be found or cannot be imported.
Undefined function 'from' for input arguments of type 'char'.
I keep getting the same error. Am I using the wrong import statement?
EDIT:
I noticed something else. when i browse through my C:/ drive and look inside the folders where Pip, and PIL ,etc is. There is nothing inside them. Even when i copy a file with stuff to that area, and open that new file, i cant see any of the stuff inside. When i copy it though, and the green bar goes it shows data being transferred.
The Code:
https://hub.gke.mybinder.org/user/alexmill-website_notebooks-fcqkeh7a/notebooks/fourier-spinning-circles.ipynb#
Example:
enter code here
from PIL import Image, ImageEnhance
# Main imports/Initialization
%matplotlib inline
import matplotlib
from matplotlib import animation, rc
import matplotlib.pyplot as plt
from matplotlib.pyplot import imshow
from IPython.display import HTML
import numpy as np
from PIL import Image, ImageEnhance
import requests
from io import BytesIO
from copy import deepcopy
from scipy.spatial import distance
from scipy.interpolate import UnivariateSpline
from copy import deepcopy
# Default figure size in notebook
matplotlib.rcParams['figure.figsize'] = (6,6)
matplotlib.rcParams['image.aspect'] = 'equal'
---------------------------------------------------------------------------
ModuleNotFoundError Traceback (most recent call last)
<ipython-input-16-7c75157a7886> in <module>
7 from IPython.display import HTML
8 import numpy as np
----> 9 from PIL import Image, ImageEnhance
10 import requests
11 from io import BytesIO
ModuleNotFoundError: No module named 'PIL'
The reason your binder doesn't work is because your requirements.txt looks like this:
matplotlib==2.0.2
requests==2.20.0
Pillow==5.0.0
numpy==1.13.0
scipy==0.19.0
pandas==0.20.2
scikit-learn==0.18.1
tzlocal-1.5.1
This line:
tzlocal-1.5.1
Probably breaks the binder dependency install. Another thing is that these are very specific versions that might generate too specific dependencies (ref. here). I recommend changing it to this:
matplotlib
requests
pillow
numpy
scipy
pandas
scikit-learn
tzlocal
At least your binder started working when I did that.
Locally you will just have to run pip install -r requirements.txt with an admin command prompt and then your local Jupyter installation should automatically work, at least it did when I cloned your notebook to a Windows VM.
I have this code in Spyder (Python 3.7):
import numpy as np
import matplotlib.pyplot as plt
#%% Práctica 1
%reset -f
plt.close("all")
If I only run one of the commands (any of them) works well, but I can't run both of them at the same time (Then, I get the error name 'plt' is not defined althought I have run the import command). I don't have any clue about what can happen.
I hope someone can help me. Thank you for your answers.
Change the order of commands:
%reset -f
import numpy as np
import matplotlib.pyplot as plt
plt.close("all")
When I run the following code in PyCharm on a Mac:
import numpy as np
import pandas as pd
from subprocess import check_output
print(check_output(["ls", "../input"]).decode("utf8"))
import time
import copy
import numpy as np
import pandas as pd
import chainer
import chainer.functions as F
import chainer.links as L
from plotly import tools
from plotly.graph_objs import *
from plotly.offline import init_notebook_mode, iplot, iplot_mpl
init_notebook_mode()
data = pd.read_csv('../input/Data/Stocks/goog.us.txt')
data['Date'] = pd.to_datetime(data['Date'])
data = data.set_index('Date')
print(data.index.min(), data.index.max())
data.head()
There were some errors:
UserWarning: Accelerate has been detected as a NumPy backend library.
vecLib, which is a part of Accelerate, is known not to work correctly with Chainer.
We recommend using other BLAS libraries such as OpenBLAS.
For details of the issue, please see
https://docs.chainer.org/en/stable/tips.html#mnist-example-does-not-converge-in-cpu-mode-on-mac-os-x.
Please be aware that Mac OS X is not an officially supported OS.
''') # NOQA
Traceback (most recent call last):
File "/Users/yindeyong/Desktop/PythonProjects/pythonstock/DQNStcok.py", line 33, in <module>
init_notebook_mode()
File "/Users/yindeyong/Desktop/PythonProjects/envs/stockenv/lib/python3.6/site-packages/plotly/offline/offline.py", line 250, in init_notebook_mode
raise ImportError('`iplot` can only run inside an IPython Notebook.')
ImportError: `iplot` can only run inside an IPython Notebook.
Process finished with exit code 1
I'm new in Chainer and DQN. Could anyone help me edit this code to make it work? Thank you so much!
It seems like you're trying to run plotly interactive functions (i prefix) in a normal Python code (i.e. not IPython Notebook). iplot provides an interactive graph with which you can play inside the notebook.
I'd start with removing iplot import and replacing it with normal plot. Also, remove iplot_mpl and init_notebook_mode from your imports.
You can install 'ipython' in the virtual environment and then try running it directly from the terminal.
Activate the virtual environment
pip3 install ipython
python3 prog.py
Worked in my case.
I am using below code in my jupyter notebook.
import pandas as pd
import numpy as np
%matplotlib inline
from plotly import __version__
from plotly.offline import download_plotlyjs, init_notebook_mode, plot, iplot
print(__version__)
import cufflinks as cf
init_notebook_mode(connected=True)
cf.go_offline()
df2 = pd.DataFrame({'Category':['A','B','C'],'Values':[22,33,45]})
df2.iplot(kind='bar',x='Category',y='Values', asImage=True, filename='bar')
Its giving an image to save while running in notebook, and i saved this code in bar.py and I ran python bar.py it's giving me the error You must be authenticated to generate an image via json.
I want to run my script which will save my bar plot as image in the same location, I can't use notebook because this scripts is going to be in my automation.
The Plotly graphs are generated in HTML+Javascript. When you run in Jupyter Notebook, you're in a web application that runs in the browser already, so it can render them directly.
When running on the command line, it can generate an HTML file with the graph for you, but you'll need to open that in the browser to have it rendered.
The Plotly Offline documentation page explains this. The text there says that you can save images only when running in Notebook. There seems to be a way to generate an image using online mode - you will need a Plotly account and network access for that.
You might want to consider a different plot library for automated offline work, which does not require network access or running an HTML user agent.
tried this https://plot.ly/python/static-image-export/ and volla!
though I had to struggle with orca.
import pandas as pd
import numpy as np
%matplotlib inline
from plotly import __version__
from plotly.offline import download_plotlyjs, init_notebook_mode, plot, iplot
import plotly.graph_objs as go
import plotly.io as pio
print(__version__)
import cufflinks as cf
init_notebook_mode(connected=True)
cf.go_offline()
fig = go.Figure()
fig.add_bar(x=df2['Category'],y=df2['Values'])
iplot(fig)
pio.write_image(fig, 'fig1.png')
I have plotted a graph from a CSV file using SPYDER from PYTHON
Here is my code
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
df = pd.read_csv('GM_NB_Replica_Test.csv', skiprows=8)
data = df.ix[:,19:116].T
data.columns=df['SN']
data.plot()
plt.show()`
Here is my Output inside the python console:
How do i get this to run externally (Not inside python console, and not in new python console)
I have already tried changing the run settings (See photo below)
However it just gives me a black Python CMD and then no graph.
any thoughts?
current error when running in CMD
From your command prompt (CMD):
cd C://location_of_your_script
python script.py