While being able to write documents with all of these built-in features from the previous chapter is all well and good, one of the biggest draws of Quarto is being able to put all of your code to process and visualise your data inside the document, removing the need for any copying-and-pasting.
Tip
As your Quarto documents get bigger and more complex, it can be a good idea to move some of the code out into separate files to make it neater and easier to debug - but for now, we’ll just assume that we’re putting the end-to-end code inside the documents.
In the next chapter, we’ll look at how to set these parameters on a per-document level - though you may find yourself wanting to overwrite the default you set for certain cells, so it’s useful to know both ways of doing it.
For multipage documents like books, you may even want to set these parameters at a project level.
17.3 Making the most of the persistent environment
As we move through the document, any variables we have created will persist.
Therefore, in the later steps, we can refer to the same variables and reuse the same libraries without having to recreate them.
```{python}import foliumgp_list = pd.read_csv("https://github.com/hsma-programme/h6_3b_advanced_qgis_mapping_python/raw/main/h6_3b_advanced_qgis_and_mapping_in_python/example_code/gp_surgery_locations_plus_patient_list_size.csv")gp_list_gdf = geopandas.GeoDataFrame( gp_list, # Our pandas dataframe geometry = geopandas.points_from_xy( gp_list['result_eastings'], # Our 'x' column (horizontal position of points) gp_list['result_northings'] # Our 'y' column (vertical position of points) ), crs = 'EPSG:27700' )# Convert to 4326 (lat/long) for working with Foliumgp_list_gdf = gp_list_gdf.to_crs('EPSG:4326')# Filter out instances with no 'list' (e.g. things like specialist clinics)gp_list = gp_list[~gp_list['Total List Size'].isna()]# reduce to the southwest to not overload Foliumxmin, xmax = -6.449974,-2.717735ymin, ymax = 49.814737,51.246969gp_list_gdf_sw = gp_list_gdf.cx[xmin:xmax, ymin:ymax]# Filter out instances with no geometrygp_list_gdf_sw = gp_list_gdf_sw[~gp_list_gdf_sw['geometry'].is_empty]# Create a geometry list from the GeoDataFramegeo_df_list = [[point.xy[1][0], point.xy[0][0]] for point in gp_list_gdf_sw.geometry]gp_map_tooltip = folium.Map( location=[50.7, -4.2], zoom_start=8, tiles='openstreetmap', )for i, coordinates in enumerate(geo_df_list): gp_map_tooltip = gp_map_tooltip.add_child( folium.Marker( location=coordinates, tooltip=gp_list_gdf_sw['name'].values[i], icon=folium.Icon(icon="user-md", prefix='fa', color="black") ) )gp_map_tooltip```
Make this Notebook Trusted to load map: File -> Trust Notebook
17.6 Additional Code Chunk Options
Like #| echo: false, there are a range of different code chunk options that can be set.
Each value needs to be set as #| parameter: value on its own line at the start of the code block.
The key ones are
eval
this determines whether the cell will actually run or not
true or false
echo
this determines whether the code is included
true or false
code-fold
this allows you to include the code as a collapsible element
true (put it in a collapsible element and collapse it), false (don’t collapse - default), show (put it in a collapsible element but leave it expanded by default)
label
this gives the code cell an internal label that will appear in the terminal printout when the code is being rendered, which can make it a lot easier to work out which code cell is the culprit if you receive errors
a-continuous-string-describing-the-cell
warning
this determines whether warning messages outputted by any of the actions in the cell will be included in the output
true/false
output
this determines whether the output of the code is actually shown. Can be useful for preprocessing steps you want to execute
true/false/asis
asis is a special parameter we’ll cover more later - it can be helpful for producing, for example, automated tabsets in a loop