To edit/execute a Homework/lecture notebook:
- Open the page for the Homework/Lecture on this site.
- For example: Homework 0
- Click the launch button (🚀) at the top.
{% if id == "nyu" -%}
- {{coding_env_name}} may take a few minutes to start up. {%- endif %}
- You should now see the notebook in {{coding_env_name}}. {% if id == "columbia" -%}
- Make sure you're using your Columbia Google account. How to switch.
- Click
Copy to Drive
. {% endif -%}
That is now your own copy; make edits in there directly.
{% if id == "nyu" -%}
- All lecture slides and homework templates can be found under
python-public-policy/
. The contents of this directory will be automatically updated from the GitHub repository, but should keep any changes you make.- FYI that this uses nbgitpuller under the hood.
- Access {{coding_env_name}} via the links on this site rather than bookmarking {{coding_env_name}}, as that will pull down the latest changes. {% endif -%}
- Read the instructions carefully. Like word problems from math class, they are very specific in what they are asking for.
- Spot check your results. If you are transforming data from a previous Step, compare the results, do a handful of the calculations manually, etc. to ensure that the results are correct.
- You are always welcome to add cells. You probably don't want more than a few lines of code in each. This makes the spot checking of intermediate results easier.
- Don't repeat yourself (DRY). If you find yourself copying and pasting code within a notebook, there's probably a better way to do it.
- Avoid hard-coding values. Don't rely on things like row numbers or column order being stable, in case the dataset were to be updated.
- Ask for help.
- The assignments are meant to be challenging, not impossible.
- Try and work through problems on your own to start. If you are stuck for more than a half hour, step away. If you still can't figure it out, ask for help.
{% if id == "columbia" -%} To keep data between sessions, you'll probably want to store it in Google Drive.
- Upload the file(s) somewhere in Drive.
- In the Colab sidebar, click the
Files
icon. - Click the
Mount Drive
icon.- Think of this as attaching your Drive to your Colab instance, as if you were plugging in a USB flash drive.
- Navigate to the file.
- Next to the filename, click the three dots.
- Click
Copy path
.- The value should be something like
/content/drive/My Drive/...
.
- The value should be something like
- Use this path with
read_csv()
. {% else -%} - Open the {{coding_env_name}} file browser.
- Navigate to the folder your notebook is in.
- Upload the data.
- From Python, use
read_csv("./<filename>.csv")
.
Note that that file path should be to relative to the notebook within {{coding_env_name}} — ./
means "in the same directory". {{coding_env_name}} cannot access the file on your local machine; in other words, the path shouldn't start with C:\\
or anything like that. More info about file paths
{{coding_env_name}} has a disk storage limit of 1GB (a.k.a. 1,024 MB or 1,048,576 KB) across all your files, and a memory limit of 3GB. {%- endif %}
You can make data smaller before uploading by filtering it through:
- The data portal, if it supports it
- This makes the download faster, including only the data you need.
- Instructions for Socrata-based portals
- In a spreadsheet program
{% if id == "nyu" -%}
See also: course generative AI policy.
We will be using the AI magic.
Note that we only have the gpt-4.0-32k
model enabled.
-
One-time setup: Visit projects.rit.nyu.edu and accept the Terms of Use.
- This site is only accessible from the NYU network.
- If you're off campus, you can connect via the VPN.
- This site is only accessible from the NYU network.
-
Open {{coding_env_name}}.
-
Load the models by running the following in a
Code
cell:%load_ext rit_jupyter_ai_magics
-
In another cell, run:
%%ai gpt-4.0-32k how are you?
-
Replace
how are you?
with your prompt. You can repeat that cell as many times as you need. {%- endif %}
- Ensure all the outputs are visible and the notebook is cleaned up.
- This is a good time to run the notebook end-to-end with
Restart and run all
{% if id == "nyu" %} (⏩){% endif %}. - See general scoring criteria. {% if id == "columbia" -%}
- This is a good time to run the notebook end-to-end with
- Confirm that the notebook is shared with the instructor and {{assistant_name}} with
Commenter
permissions. If it isn't, share the parent folder and re-confirm. - Copy the URL of your notebook.
- The URL should be of the format
https://colab.research.google.com/drive/<long identifier>
. If it'shttps://colab.research.google.com/github/...
, clickCopy to Drive
.
- The URL should be of the format
- Paste your notebook URL in the {{lms_name}} Assignment.
Engaging with comments left in {{coding_env_name}} is more than welcome. After the late submission deadline for a given Homework passes, the solution will be shared. {% else -%}
- Export the notebook as a PDF. From the Jupyter interface, go to:
File
Save and Export Notebook As…
PDF
- You may need to allow popups
- Glance through the PDF to ensure everything is showing up as you intend.
- In particular, check your visualizations.
- What you see is what the instructors will see.
- Troubleshooting tips
- If one of the Homeworks: Upload the PDF to the {{lms_name}} Assignment.
- If the Final Project:
- In {{lms_name}}, go to
Content
, thenFinal Project
. You should see the TurnItIn/PeerMark dashboard. - Follow these instructions to upload the PDF.
- In {{lms_name}}, go to
When you're ready to have it formally re-graded, please resubmit through the same Assignment in {{lms_name}}. {%- endif %}
Note: In-class exercises will not be graded.
{% if id == "columbia" -%}
-
Mounting Google Drive is slow or fails: See the Google Colab help page.
-
Can't load a file from Drive with
requests.get()
: Useopen()
.- How to read a JSON file {% else -%}
-
Error: Command '['git', 'diff', '..origin/nyu', '--name-status']' returned non-zero exit status 128
when trying to launch {{coding_env_name}}: Something got corrupted in your copy of the files that come throughnbgitpuller
. Easiest thing is to move the existing ones and restart with a fresh copy.- Launch {{coding_env_name}}.
- There should be a
python-public-policy
folder. Rename it, something likepython-public-policy-old
.
- If you've edited any of the template notebooks, you can find them in that
-old
folder.
- Repeat the getting started steps. {% endif -%}
-
When using
choropleth_map()
, nothing appears on the map: Make sure:- Your
locations
corresponds to the DataFrame column name andfeatureidkey
is set toproperties.<property name>
matching the GeoJSON - The column and the GeoJSON properties have values that match
- Your
-
SettingWithCopyWarning
: How to fix -
input()
stuck: Jupyter can be a bit buggy when dealing with interactive input. If it seems to get stuck or you aren't seeing a prompt when you'd expect one, try clicking the{{coding_env_kernel_name.title()}}
menu thenRestart Kernel
. -
The values are out of order along the axis of a Plotly chart: Make sure that:
- The values are integers/floats/timestamps, not strings, where applicable.
- Line charts: The column used for the X axis is sorted.
-
AttributeError: partially initialized module 'orjson' has no attribute 'OPT_NON_STR_KEYS'
: Add the following cell before your other Plotly code, thenRestart and run all
{% if id == "nyu" %} (⏩){% endif %}.import plotly.io plotly.io.json.config.default_engine = 'json'
{% if id == "nyu" -%}
If you get an error of Disk is full
/ No space left on device
/ Out of diskspace
: You've used all the available disk space. If you do fill it up, your server may not be able to start again (spawn failed
). You'll need to delete one or more large files that you don't need anymore:
- If you server is started already (you're seeing notebooks), click
Control Panel
->Stop My Server
. - Go to start your server again.
- Select
Troubleshooting Only - Clear Disk
. - Look at the
File size
Jupyter shows in the file browser. - Delete one or more large files.
- If you're still using those datasets, make them smaller.
This error can happen if you tried to output a lot of data in tables/charts. Steps to resolve:
-
Open the {{coding_env_name}}) file browser
-
Click
New
, thenTerminal
-
Run the following, changing the path at the end to match whatever notebook needs to be repaired:
jupyter nbconvert --to notebook --clear-output ~/python-public-policy/hw_<NUMBER>.ipynb
If you're confused by these instrucions, download the notebook file and email to the instructor. {%- endif %}
The {{coding_env_kernel_name}} is the place where Python is installed and the code is actually executing, in the cloud somewhere.
{% if id == "nyu" -%}
- Make sure
Python [conda env:python-public-policy]
is selected as the {{coding_env_kernel_name}}.- Shows in the top right of the notebook interface
- To change:
- Open the
{{coding_env_kernel_name.title()}}
menu - Click
Change {{coding_env_kernel_name}}
- Click
Python [conda env:python-public-policy]
{%- endif %}
- Open the
- If your {{coding_env_kernel_name}} is repeatedly crashing, you're probably running out of memory.
- Make sure you aren't loading data sets you don't need.
- If loading a new dataset, make it smaller {% if id == "nyu" %}- Close {{coding_env_kernel_name}}s you aren't using from the Running page.{% endif %}
{% if id == "nyu" -%}
Jupyter notebook export to PDF is fragile, especially with interactive charts through Plotly.
If you get a 500 error, it could be happening for a handful of reasons. Scroll to the bottom and read the message.
- If it mentions
Undefined control sequence
and\pandocbounded
, it's an issue with the exporter. Jump to the Alternatives. - If it mentions a package being missing, ask on Ed.
- Otherwise, it may be crashing due to not being handle the complexity of the render. Try reducing your output (in the Jupyter sense) to smaller subsets. This can include:
- Not displaying so many rows/values
- Reducing the number of points that are plotted
If you are unable to get the direct-to-PDF export working, try:
- Going through HTML:
- Export the notebook as HTML.
- Open the HTML file in your browser.
File
->Print…
Save as PDF
- If any of the visualizations are cut off:
- Cancel the Print to PDF.
- Viewing the HTML file, make the browser window narrower.
- This forces the visualizations to re-draw.
- Try from
File
->Print…
again.
- An online converter, such as Ploomer {%- endif %}