Python intro#
gdsfactory is written in python and requires some basic knowledge of python.
If you are new to python you can find many resources online
This notebook is for you to experiment with some common python patterns in gdsfactory
Classes#
Gdsfactory has already some pre-defined classes for you.
All the other classes (Component, ComponentReference, Port …) are already available in gf.typings
Classes are good for keeping state, which means that they store some information inside them (polygons, ports, references …).
In gdsfactory you will write functions instead of classes. Functions are easier to write and combine, and have clearly defined inputs and outputs.
from functools import partial
from pydantic import validate_call
import gdsfactory as gf
c = gf.Component()
c.add_polygon([(-8, -6), (6, 8), (7, 17), (9, 5)], layer=(1, 0))
c.plot()
Functions#
Functions have clear inputs and outputs, they usually accept some parameters (strings, floats, ints …) and return other parameters
def double(x):
return 2 * x
x = 1.5
y = double(x)
print(y)
3.0
It’s also nice to add type annotations
to your functions to clearly define what are the input/output types (string, int, float …)
def double(x: float) -> float:
return 2 * x
Factories#
A factory is a function that returns an object. In gdsfactory many functions return a Component
object
def bend(radius: float = 5) -> gf.Component:
return gf.components.bend_euler(radius=radius)
component = bend(radius=10)
print(component)
component.plot()
vinsts=[] info=Info(length=16.637, dy=10.0, min_bend_radius=7.061, radius=10.0, width=0.5, route_info_type='strip', route_info_length=16.637, route_info_weight=16.637, route_info_strip_length=16.637, route_info_n_bend_90=1.0, route_info_min_bend_radius=7.061) kcl=KCLayout(name='DEFAULT', layout=<klayout.dbcore.Layout object at 0x7f6cd81116d0>, layer_enclosures=LayerEnclosureModel(root={'78687732': LayerEnclosure(layer_sections={}, main_layer=WG (1/0), yaml_tag='!Enclosure')}), cross_sections={'78687732_500': SymmetricalCrossSection(width=500, enclosure=LayerEnclosure(layer_sections={}, main_layer=WG (1/0), yaml_tag='!Enclosure'), name='78687732_500')}, enclosure=KCellEnclosure(enclosures=LayerEnclosureCollection(enclosures=[])), library=<klayout.dbcore.Library object at 0x7f6cd8111630>, factories={'taper': <function taper at 0x7f6cc2b0c5e0>, 'bend_s_bezier': <function bend_s_bezier_factory.<locals>.bend_s_bezier at 0x7f6cce45ac00>, 'bend_circular': <function bend_circular at 0x7f6cc2ceaa20>, 'bend_euler': <function bend_euler at 0x7f6cc2ceb420>, 'bend_s_euler': <function bend_s_euler_factory.<locals>.bend_s_euler at 0x7f6ccce89440>, 'straight': <function straight at 0x7f6cc2b6fd80>, 'rotate': <function rotate at 0x7f6cc3389580>, 'from_image': <function from_image at 0x7f6cc3397b00>, 'floorplan_with_block_letters': <function floorplan_with_block_letters at 0x7f6cc2c6a020>, 'bend_circular_heater': <function bend_circular_heater at 0x7f6cc2ceaca0>, 'bend_euler_s': <function bend_euler_s at 0x7f6cc2ceb2e0>, 'bezier': <function bezier at 0x7f6cc2ceb9c0>, 'bend_s': <function bend_s at 0x7f6cc2cebba0>, 'add_fiber_array_optical_south_electrical_north': <function add_fiber_array_optical_south_electrical_north at 0x7f6cc2cebe20>, 'ramp': <function ramp at 0x7f6cc2b0c360>, 'taper_strip_to_ridge': <function taper_strip_to_ridge at 0x7f6cc2b0c680>, 'taper_strip_to_ridge_trenches': <function taper_strip_to_ridge_trenches at 0x7f6cc2b0c900>, 'taper_sc_nc': <function taper_sc_nc at 0x7f6cc2b0ca40>, 'taper_adiabatic': <function taper_adiabatic at 0x7f6cc2b0cd60>, 'taper_cross_section': <function taper_cross_section at 0x7f6cc2b0cf40>, 'taper_from_csv': <function taper_from_csv at 0x7f6cc2b0d120>, 'taper_parabolic': <function taper_parabolic at 0x7f6cc2b0d260>, 'add_termination': <function add_termination at 0x7f6cc2cebf60>, 'add_trenches': <function add_trenches at 0x7f6cc2b0d080>, 'array': <function array at 0x7f6cc2b0d440>, 'copy_layers': <function copy_layers at 0x7f6cc2b0d940>, 'extend_ports_list': <function extend_ports_list at 0x7f6cc2b0da80>, 'extend_ports': <function extend_ports at 0x7f6cc2b0dda0>, 'pack_doe': <function pack_doe at 0x7f6cc2b0e5c0>, 'pack_doe_grid': <function pack_doe_grid at 0x7f6cc2b0e700>, 'splitter_chain': <function splitter_chain at 0x7f6cc2b0e840>, 'mzi': <function mzi at 0x7f6cc2b0ede0>, 'mzi_lattice': <function mzi_lattice at 0x7f6cc2b0f100>, 'mzi_lattice_mmi': <function mzi_lattice_mmi at 0x7f6cc2b0f240>, 'mzi_pads_center': <function mzi_pads_center at 0x7f6cc2b0f4c0>, 'mzit': <function mzit at 0x7f6cc2b0f6a0>, 'mzit_lattice': <function mzit_lattice at 0x7f6cc2b0f7e0>, 'splitter_tree': <function splitter_tree at 0x7f6cc2b0f380>, 'coupler_symmetric': <function coupler_symmetric at 0x7f6cc2b0f9c0>, 'coupler_straight': <function coupler_straight at 0x7f6cc2b0fb00>, 'coupler': <function coupler at 0x7f6cc2b0fc40>, 'coupler90': <function coupler90 at 0x7f6cc2b0fe20>, 'coupler90bend': <function coupler90bend at 0x7f6cc2b0fec0>, 'coupler_adiabatic': <function coupler_adiabatic at 0x7f6cc2b3c220>, 'coupler_asymmetric': <function coupler_asymmetric at 0x7f6cc2b3c4a0>, 'coupler_bent_half': <function coupler_bent_half at 0x7f6cc2b3c7c0>, 'coupler_bent': <function coupler_bent at 0x7f6cc2b3c900>, 'coupler_broadband': <function coupler_broadband at 0x7f6cc2b3cb80>, 'coupler_full': <function coupler_full at 0x7f6cc2b3cd60>, 'coupler_ring': <function coupler_ring at 0x7f6cc2b3cfe0>, 'coupler_straight_asymmetric': <function coupler_straight_asymmetric at 0x7f6cc2b3d260>, 'align_wafer': <function align_wafer at 0x7f6cc2b3d620>, 'add_frame': <function add_frame at 0x7f6cc2b3d6c0>, 'die': <function die at 0x7f6cc2b3d9e0>, 'die_with_pads': <function die_with_pads at 0x7f6cc2b3dbc0>, 'seal_ring': <function seal_ring at 0x7f6cc2b3dda0>, 'seal_ring_segmented': <function seal_ring_segmented at 0x7f6cc2b3dee0>, 'wafer': <function wafer at 0x7f6cc2b3e0c0>, 'free_propagation_region': <function free_propagation_region at 0x7f6cc2b3e520>, 'awg': <function awg at 0x7f6cc2b3e5c0>, 'dbr_cell': <function dbr_cell at 0x7f6cc2b3e8e0>, 'dbr': <function dbr at 0x7f6cc2b3ea20>, 'dbr_tapered': <function dbr_tapered at 0x7f6cc2b3eca0>, 'bbox': <function bbox at 0x7f6cc2b3f420>, 'C': <function C at 0x7f6cc2b3f600>, 'circle': <function circle at 0x7f6cc2b3f7e0>, 'compass': <function compass at 0x7f6cc2b3fa60>, 'cross': <function cross at 0x7f6cc2b3fc40>, 'ellipse': <function ellipse at 0x7f6cc2b3fe20>, 'fiducial_squares': <function fiducial_squares at 0x7f6cc2b3ff60>, 'L': <function L at 0x7f6cc2b6c180>, 'nxn': <function nxn at 0x7f6cc2b6c360>, 'rectangle': <function rectangle at 0x7f6cc2b6c540>, 'rectangles': <function rectangles at 0x7f6cc2b6c680>, 'regular_polygon': <function regular_polygon at 0x7f6cc2b6c7c0>, 'triangle': <function triangle at 0x7f6cc2b6c9a0>, 'triangle2': <function triangle2 at 0x7f6cc2b6cae0>, 'triangle4': <function triangle4 at 0x7f6cc2b6cc20>, 'fiber': <function fiber at 0x7f6cc2b3efc0>, 'fiber_array': <function fiber_array at 0x7f6cc2b6ccc0>, 'crossing_arm': <function crossing_arm at 0x7f6cc2b6f6a0>, 'crossing': <function crossing at 0x7f6cc2b6f7e0>, 'crossing_linear_taper': <function crossing_linear_taper at 0x7f6cc2b6f920>, 'crossing_etched': <function crossing_etched at 0x7f6cc2b6fa60>, 'crossing45': <function crossing45 at 0x7f6cc2b6fba0>, 'straight_array': <function straight_array at 0x7f6cc2b6ff60>, 'wire_straight': <function wire_straight at 0x7f6cc2ba8180>, 'straight_heater_doped_rib': <function straight_heater_doped_rib at 0x7f6cc2ba8540>, 'straight_heater_doped_strip': <function straight_heater_doped_strip at 0x7f6cc2ba8720>, 'straight_heater_meander': <function straight_heater_meander at 0x7f6cc2ba8a40>, 'via': <function via at 0x7f6cc2ba9260>, 'via_circular': <function via_circular at 0x7f6cc2ba93a0>, 'via_chain': <function via_chain at 0x7f6cc2ba96c0>, 'via_corner': <function via_corner at 0x7f6cc2ba98a0>, 'via_stack': <function via_stack at 0x7f6cc2ba9a80>, 'via_stack_corner45': <function via_stack_corner45 at 0x7f6cc2ba9bc0>, 'via_stack_corner45_extended': <function via_stack_corner45_extended at 0x7f6cc2ba9d00>, 'via_stack_with_offset': <function via_stack_with_offset at 0x7f6cc2ba9e40>, 'straight_heater_meander_doped': <function straight_heater_meander_doped at 0x7f6cc2ba8f40>, 'straight_heater_metal_undercut': <function straight_heater_metal_undercut at 0x7f6cc2ba9da0>, 'straight_heater_metal_simple': <function straight_heater_metal_simple at 0x7f6cc2baa0c0>, 'straight_piecewise': <function straight_piecewise at 0x7f6cc2baa200>, 'straight_pin': <function straight_pin at 0x7f6cc2baa2a0>, 'straight_pin_slot': <function straight_pin_slot at 0x7f6cc2baa520>, 'wire_corner': <function wire_corner at 0x7f6cc2baa7a0>, 'wire_corner45': <function wire_corner45 at 0x7f6cc2baa8e0>, 'wire_corner_sections': <function wire_corner_sections at 0x7f6cc2baa980>, 'loop_mirror': <function loop_mirror at 0x7f6cc2b6cea0>, 'mode_converter': <function mode_converter at 0x7f6cc2bc8400>, 'polarization_splitter_rotator': <function polarization_splitter_rotator at 0x7f6cc2bc8720>, 'terminator': <function terminator at 0x7f6cc2bc8cc0>, 'grating_coupler_array': <function grating_coupler_array at 0x7f6cc2bc8f40>, 'grating_coupler_dual_pol': <function grating_coupler_dual_pol at 0x7f6cc2bc9260>, 'grating_coupler_elliptical': <function grating_coupler_elliptical at 0x7f6cc2bc98a0>, 'grating_coupler_elliptical_arbitrary': <function grating_coupler_elliptical_arbitrary at 0x7f6cc2bc9b20>, 'grating_coupler_elliptical_uniform': <function grating_coupler_elliptical_uniform at 0x7f6cc2bc9da0>, 'grating_coupler_elliptical_lumerical': <function grating_coupler_elliptical_lumerical at 0x7f6cc2bc9ee0>, 'grating_coupler_elliptical_trenches': <function grating_coupler_elliptical_trenches at 0x7f6cc2bc9300>, 'grating_coupler_loss': <function grating_coupler_loss at 0x7f6cc2bca0c0>, 'grating_coupler_rectangular': <function grating_coupler_rectangular at 0x7f6cc2bca2a0>, 'grating_coupler_rectangular_arbitrary': <function grating_coupler_rectangular_arbitrary at 0x7f6cc2bca520>, 'grating_coupler_tree': <function grating_coupler_tree at 0x7f6cc2bca7a0>, 'mmi': <function mmi at 0x7f6cc2bcaac0>, 'mmi1x2': <function mmi1x2 at 0x7f6cc2bcade0>, 'mmi1x2_with_sbend': <function mmi1x2_with_sbend at 0x7f6cc2bcafc0>, 'mmi2x2': <function mmi2x2 at 0x7f6cc2bcb2e0>, 'mmi2x2_with_sbend': <function mmi2x2_with_sbend at 0x7f6cc2bcb420>, 'mmi_90degree_hybrid': <function mmi_90degree_hybrid at 0x7f6cc2bcb600>, 'mmi_tapered': <function mmi_tapered at 0x7f6cc2bcb920>, 'pad': <function pad at 0x7f6cc2bcbba0>, 'pad_array': <function pad_array at 0x7f6cc2bcbe20>, 'pad_gsg_short': <function pad_gsg_short at 0x7f6cc2bf80e0>, 'pads_shorted': <function pads_shorted at 0x7f6cc2bf8360>, 'rectangle_with_slits': <function rectangle_with_slits at 0x7f6cc2bf84a0>, 'cavity': <function cavity at 0x7f6cc2bf8720>, 'cdsem_all': <function cdsem_all at 0x7f6cc2bf89a0>, 'cdsem_bend180': <function cdsem_bend180 at 0x7f6cc2bf8b80>, 'cdsem_coupler': <function cdsem_coupler at 0x7f6cc2bf8cc0>, 'cdsem_straight': <function cdsem_straight at 0x7f6cc2bf8fe0>, 'cdsem_straight_density': <function cdsem_straight_density at 0x7f6cc2bf9120>, 'bendu_double': <function bendu_double at 0x7f6cc2bf93a0>, 'straight_double': <function straight_double at 0x7f6cc2bf94e0>, 'cutback_2x2': <function cutback_2x2 at 0x7f6cc2bf9620>, 'cutback_bend': <function cutback_bend at 0x7f6cc2bf99e0>, 'cutback_bend90': <function cutback_bend90 at 0x7f6cc2bf9b20>, 'staircase': <function staircase at 0x7f6cc2bf9c60>, 'cutback_bend180': <function cutback_bend180 at 0x7f6cc2bf9da0>, 'cutback_component': <function cutback_component at 0x7f6cc2bf9ee0>, 'cutback_splitter': <function cutback_splitter at 0x7f6cc2bfa2a0>, 'greek_cross': <function greek_cross at 0x7f6cc2bfa3e0>, 'greek_cross_with_pads': <function greek_cross_with_pads at 0x7f6cc2bfa660>, 'litho_calipers': <function litho_calipers at 0x7f6cc2bfa840>, 'litho_ruler': <function litho_ruler at 0x7f6cc2bfab60>, 'litho_steps': <function litho_steps at 0x7f6cc2bfad40>, 'resistance_meander': <function resistance_meander at 0x7f6cc2bfae80>, 'resistance_sheet': <function resistance_sheet at 0x7f6cc2bfb060>, 'verniers': <function verniers at 0x7f6cc2bfb240>, 'text': <function text at 0x7f6cc2bfbf60>, 'text_lines': <function text_lines at 0x7f6cc273c180>, 'text_klayout': <function text_klayout at 0x7f6cc273c2c0>, 'text_freetype': <function text_freetype at 0x7f6cc273c5e0>, 'pixel_array': <function pixel_array at 0x7f6cc273c860>, 'text_rectangular': <function text_rectangular at 0x7f6cc273c9a0>, 'text_rectangular_multi_layer': <function text_rectangular_multi_layer at 0x7f6cc273cae0>, 'pixel': <function pixel at 0x7f6cc2bfb560>, 'qrcode': <function qrcode at 0x7f6cc273c7c0>, 'version_stamp': <function version_stamp at 0x7f6cc273cb80>, 'disk': <function disk at 0x7f6cc273d120>, 'disk_heater': <function disk_heater at 0x7f6cc273d260>, 'ring': <function ring at 0x7f6cc273d4e0>, 'ring_crow': <function ring_crow at 0x7f6cc273d6c0>, 'ring_crow_couplers': <function ring_crow_couplers at 0x7f6cc273d800>, 'ring_double': <function ring_double at 0x7f6cc273d9e0>, 'ring_double_bend_coupler': <function ring_double_bend_coupler at 0x7f6cc273dd00>, 'ring_double_heater': <function ring_double_heater at 0x7f6cc273de40>, 'ring_double_pn': <function ring_double_pn at 0x7f6cc273e160>, 'ring_single_pn': <function ring_single_pn at 0x7f6cc273e2a0>, 'ring_single': <function ring_single at 0x7f6cc273e480>, 'ring_single_array': <function ring_single_array at 0x7f6cc273e5c0>, 'coupler_bend': <function coupler_bend at 0x7f6cc273e700>, 'coupler_ring_bend': <function coupler_ring_bend at 0x7f6cc273e840>, 'ring_single_bend_coupler': <function ring_single_bend_coupler at 0x7f6cc273e980>, 'ring_single_dut': <function ring_single_dut at 0x7f6cc273eb60>, 'delay_snake': <function delay_snake at 0x7f6cc273ede0>, 'delay_snake2': <function delay_snake2 at 0x7f6cc273efc0>, 'delay_snake_sbend': <function delay_snake_sbend at 0x7f6cc273f240>, 'spiral': <function spiral at 0x7f6cc273f4c0>, 'spiral_double': <function spiral_double at 0x7f6cc273f6a0>, 'spiral_racetrack': <function spiral_racetrack at 0x7f6cc273f920>, 'spiral_racetrack_fixed_length': <function spiral_racetrack_fixed_length at 0x7f6cc273fa60>, 'spiral_racetrack_heater_metal': <function spiral_racetrack_heater_metal at 0x7f6cc273fc40>, 'spiral_racetrack_heater_doped': <function spiral_racetrack_heater_doped at 0x7f6cc273fd80>, 'spiral_inductor': <function spiral_inductor at 0x7f6cc273fec0>, 'hline': <function hline at 0x7f6cc2770180>, 'optimal_90deg': <function optimal_90deg at 0x7f6cc27702c0>, 'optimal_hairpin': <function optimal_hairpin at 0x7f6cc27705e0>, 'optimal_step': <function optimal_step at 0x7f6cc2770720>, 'snspd': <function snspd at 0x7f6cc2770900>, 'interdigital_capacitor': <function interdigital_capacitor at 0x7f6cc2770ae0>, 'ge_detector_straight_si_contacts': <function ge_detector_straight_si_contacts at 0x7f6cc2770d60>, 'edge_coupler_silicon': <function edge_coupler_silicon at 0x7f6cc2770fe0>, 'edge_coupler_array': <function edge_coupler_array at 0x7f6cc2771120>, 'edge_coupler_array_with_loopback': <function edge_coupler_array_with_loopback at 0x7f6cc2771260>}, virtual_factories={'virtual_bend_circular': <function virtual_bend_circular_factory.<locals>.virtual_bend_circular at 0x7f6ccce8a340>, 'bend_euler': <function virtual_bend_euler_factory.<locals>.bend_euler at 0x7f6ccce8a5c0>, 'virtual_straight': <function virtual_straight_factory.<locals>.virtual_straight at 0x7f6ccce8a840>, 'bend_circular_all_angle': <function bend_circular_all_angle at 0x7f6cc2ceab60>, 'bend_euler_all_angle': <function bend_euler_all_angle at 0x7f6cc2ceb6a0>, 'straight_all_angle': <function straight_all_angle at 0x7f6cc2b6fec0>}, kcells={0: Unnamed_0: ports [], 0 instances, 1: bend_euler_R10_A90_P0p5_2f1f5c6d: ports ['o1', 'o2'], 0 instances}, layers=<aenum 'LAYER'>, infos=LayerInfos(), layer_stack=LayerStack(layers={}), netlist_layer_mapping={}, sparameters_path=None, interconnect_cml_path=None, constants=Constants(), rename_function=<function rename_clockwise_multi at 0x7f6cd83936a0>, info=Info(), settings=KCellSettings(version='0.23.1', klayout_version='0.29.11', meta_format='v3'), future_cell_name=None, decorators=<kfactory.decorators.Decorators object at 0x7f6cd83fef50>) function_name='bend_euler' basename=None boundary=None insts=<gdsfactory.component.ComponentReferences object at 0x7f6ca806b150> size_info=<kfactory.kcell.SizeInfo object at 0x7f6c9a9aaf50> dsize_info=<kfactory.kcell.DSizeInfo object at 0x7f6ca3f1ee50> routes={}
Decorators#
gdsfactory has many functions, and we want to do some common operations for the ones that return a Component:
give a unique name (dependent on the input parameters) to a Component
cache the Component that the function returns for speed and reuse cells.
For that you will see a @cell
decorator on many component functions.
@validate_call
def double(x: float) -> float:
return 2 * x
x = 1.5
y = double(x)
print(y)
3.0
The validator decorator is equivalent to running
def double(x: float) -> float:
return 2 * x
double_with_validator = validate_call(double)
x = 1.5
y = double_with_validator(x)
print(y)
3.0
The cell
decorator also leverages that validate arguments.
So you should add type annotations to your component factories.
Lets try to create an error x
and you will get a clear message the the function double
does not work with strings
y = double("not_valid_number")
will raise a ValidationError
ValidationError: 0 validation error for Double
x
value is not a valid float (type=type_error.float)
It will also cast
the input type based on the type annotation. So if you pass an int
it will convert it to float
x = 1
y = double_with_validator(x)
print(y, type(x), type(y))
2.0 <class 'int'> <class 'float'>
List comprehensions#
You will also see some list comprehensions, which are common in python.
For example, you can write many loops in one line
y = []
for x in range(3):
y.append(double(x))
print(y)
[0, 2, 4]
y = [double(x) for x in range(3)] # much shorter and easier to read
print(y)
[0, 2, 4]
Functional programming#
Functional programming follows linux philosophy:
Write functions that do one thing and do it well.
Write functions to work together.
Write functions with clear inputs and outputs
partial#
Partial is an easy way to modify the default arguments of a function. This is useful in gdsfactory because we define PCells using functions. from functools import partial
You can use partial to create a new function with some default arguments.
The following two functions are equivalent in functionality. Notice how the second one is shorter, more readable and easier to maintain thanks to partial
:
def ring_sc1(gap=0.3, **kwargs) -> gf.Component:
return gf.components.ring_single(gap=gap, **kwargs)
ring_sc2 = partial(gf.components.ring_single, gap=0.3) # shorter and easier to read
As you customize more parameters, it’s more obvious that the second one is easier to maintain
def ring_sc3(gap=0.3, radius=10, **kwargs):
return gf.components.ring_single(gap=gap, radius=radius, **kwargs)
ring_sc4 = partial(gf.components.ring_single, gap=0.3, radius=10)
compose#
gf.compose
combines two functions into one. This is useful in gdsfactory because we define PCells using functions and functions are easier to combine than classes. You can also import compose from the toolz package from toolz import compose
ring_sc5 = partial(gf.components.ring_single, radius=10)
add_gratings = gf.routing.add_fiber_array
ring_sc_gc = gf.compose(add_gratings, ring_sc5)
ring_sc_gc5 = ring_sc_gc(radius=5)
ring_sc_gc5
ring_sc_gc20 = ring_sc_gc(radius=20)
ring_sc_gc20
This is equivalent and more readable than writing
ring_sc_gc5 = add_gratings(ring_sc1(radius=5))
ring_sc_gc5
ring_sc_gc20 = add_gratings(ring_sc1(radius=20))
ring_sc_gc20
print(ring_sc_gc5)
vinsts=[] info=Info() kcl=KCLayout(name='DEFAULT', layout=<klayout.dbcore.Layout object at 0x7f6cd81116d0>, layer_enclosures=LayerEnclosureModel(root={'78687732': LayerEnclosure(layer_sections={}, main_layer=WG (1/0), yaml_tag='!Enclosure')}), cross_sections={'78687732_500': SymmetricalCrossSection(width=500, enclosure=LayerEnclosure(layer_sections={}, main_layer=WG (1/0), yaml_tag='!Enclosure'), name='78687732_500'), '78687732_25946': SymmetricalCrossSection(width=25946, enclosure=LayerEnclosure(layer_sections={}, main_layer=WG (1/0), yaml_tag='!Enclosure'), name='78687732_25946'), '78687732_10000': SymmetricalCrossSection(width=10000, enclosure=LayerEnclosure(layer_sections={}, main_layer=WG (1/0), yaml_tag='!Enclosure'), name='78687732_10000')}, enclosure=KCellEnclosure(enclosures=LayerEnclosureCollection(enclosures=[])), library=<klayout.dbcore.Library object at 0x7f6cd8111630>, factories={'taper': <function taper at 0x7f6cc2b0c5e0>, 'bend_s_bezier': <function bend_s_bezier_factory.<locals>.bend_s_bezier at 0x7f6cce45ac00>, 'bend_circular': <function bend_circular at 0x7f6cc2ceaa20>, 'bend_euler': <function bend_euler at 0x7f6cc2ceb420>, 'bend_s_euler': <function bend_s_euler_factory.<locals>.bend_s_euler at 0x7f6ccce89440>, 'straight': <function straight at 0x7f6cc2b6fd80>, 'rotate': <function rotate at 0x7f6cc3389580>, 'from_image': <function from_image at 0x7f6cc3397b00>, 'floorplan_with_block_letters': <function floorplan_with_block_letters at 0x7f6cc2c6a020>, 'bend_circular_heater': <function bend_circular_heater at 0x7f6cc2ceaca0>, 'bend_euler_s': <function bend_euler_s at 0x7f6cc2ceb2e0>, 'bezier': <function bezier at 0x7f6cc2ceb9c0>, 'bend_s': <function bend_s at 0x7f6cc2cebba0>, 'add_fiber_array_optical_south_electrical_north': <function add_fiber_array_optical_south_electrical_north at 0x7f6cc2cebe20>, 'ramp': <function ramp at 0x7f6cc2b0c360>, 'taper_strip_to_ridge': <function taper_strip_to_ridge at 0x7f6cc2b0c680>, 'taper_strip_to_ridge_trenches': <function taper_strip_to_ridge_trenches at 0x7f6cc2b0c900>, 'taper_sc_nc': <function taper_sc_nc at 0x7f6cc2b0ca40>, 'taper_adiabatic': <function taper_adiabatic at 0x7f6cc2b0cd60>, 'taper_cross_section': <function taper_cross_section at 0x7f6cc2b0cf40>, 'taper_from_csv': <function taper_from_csv at 0x7f6cc2b0d120>, 'taper_parabolic': <function taper_parabolic at 0x7f6cc2b0d260>, 'add_termination': <function add_termination at 0x7f6cc2cebf60>, 'add_trenches': <function add_trenches at 0x7f6cc2b0d080>, 'array': <function array at 0x7f6cc2b0d440>, 'copy_layers': <function copy_layers at 0x7f6cc2b0d940>, 'extend_ports_list': <function extend_ports_list at 0x7f6cc2b0da80>, 'extend_ports': <function extend_ports at 0x7f6cc2b0dda0>, 'pack_doe': <function pack_doe at 0x7f6cc2b0e5c0>, 'pack_doe_grid': <function pack_doe_grid at 0x7f6cc2b0e700>, 'splitter_chain': <function splitter_chain at 0x7f6cc2b0e840>, 'mzi': <function mzi at 0x7f6cc2b0ede0>, 'mzi_lattice': <function mzi_lattice at 0x7f6cc2b0f100>, 'mzi_lattice_mmi': <function mzi_lattice_mmi at 0x7f6cc2b0f240>, 'mzi_pads_center': <function mzi_pads_center at 0x7f6cc2b0f4c0>, 'mzit': <function mzit at 0x7f6cc2b0f6a0>, 'mzit_lattice': <function mzit_lattice at 0x7f6cc2b0f7e0>, 'splitter_tree': <function splitter_tree at 0x7f6cc2b0f380>, 'coupler_symmetric': <function coupler_symmetric at 0x7f6cc2b0f9c0>, 'coupler_straight': <function coupler_straight at 0x7f6cc2b0fb00>, 'coupler': <function coupler at 0x7f6cc2b0fc40>, 'coupler90': <function coupler90 at 0x7f6cc2b0fe20>, 'coupler90bend': <function coupler90bend at 0x7f6cc2b0fec0>, 'coupler_adiabatic': <function coupler_adiabatic at 0x7f6cc2b3c220>, 'coupler_asymmetric': <function coupler_asymmetric at 0x7f6cc2b3c4a0>, 'coupler_bent_half': <function coupler_bent_half at 0x7f6cc2b3c7c0>, 'coupler_bent': <function coupler_bent at 0x7f6cc2b3c900>, 'coupler_broadband': <function coupler_broadband at 0x7f6cc2b3cb80>, 'coupler_full': <function coupler_full at 0x7f6cc2b3cd60>, 'coupler_ring': <function coupler_ring at 0x7f6cc2b3cfe0>, 'coupler_straight_asymmetric': <function coupler_straight_asymmetric at 0x7f6cc2b3d260>, 'align_wafer': <function align_wafer at 0x7f6cc2b3d620>, 'add_frame': <function add_frame at 0x7f6cc2b3d6c0>, 'die': <function die at 0x7f6cc2b3d9e0>, 'die_with_pads': <function die_with_pads at 0x7f6cc2b3dbc0>, 'seal_ring': <function seal_ring at 0x7f6cc2b3dda0>, 'seal_ring_segmented': <function seal_ring_segmented at 0x7f6cc2b3dee0>, 'wafer': <function wafer at 0x7f6cc2b3e0c0>, 'free_propagation_region': <function free_propagation_region at 0x7f6cc2b3e520>, 'awg': <function awg at 0x7f6cc2b3e5c0>, 'dbr_cell': <function dbr_cell at 0x7f6cc2b3e8e0>, 'dbr': <function dbr at 0x7f6cc2b3ea20>, 'dbr_tapered': <function dbr_tapered at 0x7f6cc2b3eca0>, 'bbox': <function bbox at 0x7f6cc2b3f420>, 'C': <function C at 0x7f6cc2b3f600>, 'circle': <function circle at 0x7f6cc2b3f7e0>, 'compass': <function compass at 0x7f6cc2b3fa60>, 'cross': <function cross at 0x7f6cc2b3fc40>, 'ellipse': <function ellipse at 0x7f6cc2b3fe20>, 'fiducial_squares': <function fiducial_squares at 0x7f6cc2b3ff60>, 'L': <function L at 0x7f6cc2b6c180>, 'nxn': <function nxn at 0x7f6cc2b6c360>, 'rectangle': <function rectangle at 0x7f6cc2b6c540>, 'rectangles': <function rectangles at 0x7f6cc2b6c680>, 'regular_polygon': <function regular_polygon at 0x7f6cc2b6c7c0>, 'triangle': <function triangle at 0x7f6cc2b6c9a0>, 'triangle2': <function triangle2 at 0x7f6cc2b6cae0>, 'triangle4': <function triangle4 at 0x7f6cc2b6cc20>, 'fiber': <function fiber at 0x7f6cc2b3efc0>, 'fiber_array': <function fiber_array at 0x7f6cc2b6ccc0>, 'crossing_arm': <function crossing_arm at 0x7f6cc2b6f6a0>, 'crossing': <function crossing at 0x7f6cc2b6f7e0>, 'crossing_linear_taper': <function crossing_linear_taper at 0x7f6cc2b6f920>, 'crossing_etched': <function crossing_etched at 0x7f6cc2b6fa60>, 'crossing45': <function crossing45 at 0x7f6cc2b6fba0>, 'straight_array': <function straight_array at 0x7f6cc2b6ff60>, 'wire_straight': <function wire_straight at 0x7f6cc2ba8180>, 'straight_heater_doped_rib': <function straight_heater_doped_rib at 0x7f6cc2ba8540>, 'straight_heater_doped_strip': <function straight_heater_doped_strip at 0x7f6cc2ba8720>, 'straight_heater_meander': <function straight_heater_meander at 0x7f6cc2ba8a40>, 'via': <function via at 0x7f6cc2ba9260>, 'via_circular': <function via_circular at 0x7f6cc2ba93a0>, 'via_chain': <function via_chain at 0x7f6cc2ba96c0>, 'via_corner': <function via_corner at 0x7f6cc2ba98a0>, 'via_stack': <function via_stack at 0x7f6cc2ba9a80>, 'via_stack_corner45': <function via_stack_corner45 at 0x7f6cc2ba9bc0>, 'via_stack_corner45_extended': <function via_stack_corner45_extended at 0x7f6cc2ba9d00>, 'via_stack_with_offset': <function via_stack_with_offset at 0x7f6cc2ba9e40>, 'straight_heater_meander_doped': <function straight_heater_meander_doped at 0x7f6cc2ba8f40>, 'straight_heater_metal_undercut': <function straight_heater_metal_undercut at 0x7f6cc2ba9da0>, 'straight_heater_metal_simple': <function straight_heater_metal_simple at 0x7f6cc2baa0c0>, 'straight_piecewise': <function straight_piecewise at 0x7f6cc2baa200>, 'straight_pin': <function straight_pin at 0x7f6cc2baa2a0>, 'straight_pin_slot': <function straight_pin_slot at 0x7f6cc2baa520>, 'wire_corner': <function wire_corner at 0x7f6cc2baa7a0>, 'wire_corner45': <function wire_corner45 at 0x7f6cc2baa8e0>, 'wire_corner_sections': <function wire_corner_sections at 0x7f6cc2baa980>, 'loop_mirror': <function loop_mirror at 0x7f6cc2b6cea0>, 'mode_converter': <function mode_converter at 0x7f6cc2bc8400>, 'polarization_splitter_rotator': <function polarization_splitter_rotator at 0x7f6cc2bc8720>, 'terminator': <function terminator at 0x7f6cc2bc8cc0>, 'grating_coupler_array': <function grating_coupler_array at 0x7f6cc2bc8f40>, 'grating_coupler_dual_pol': <function grating_coupler_dual_pol at 0x7f6cc2bc9260>, 'grating_coupler_elliptical': <function grating_coupler_elliptical at 0x7f6cc2bc98a0>, 'grating_coupler_elliptical_arbitrary': <function grating_coupler_elliptical_arbitrary at 0x7f6cc2bc9b20>, 'grating_coupler_elliptical_uniform': <function grating_coupler_elliptical_uniform at 0x7f6cc2bc9da0>, 'grating_coupler_elliptical_lumerical': <function grating_coupler_elliptical_lumerical at 0x7f6cc2bc9ee0>, 'grating_coupler_elliptical_trenches': <function grating_coupler_elliptical_trenches at 0x7f6cc2bc9300>, 'grating_coupler_loss': <function grating_coupler_loss at 0x7f6cc2bca0c0>, 'grating_coupler_rectangular': <function grating_coupler_rectangular at 0x7f6cc2bca2a0>, 'grating_coupler_rectangular_arbitrary': <function grating_coupler_rectangular_arbitrary at 0x7f6cc2bca520>, 'grating_coupler_tree': <function grating_coupler_tree at 0x7f6cc2bca7a0>, 'mmi': <function mmi at 0x7f6cc2bcaac0>, 'mmi1x2': <function mmi1x2 at 0x7f6cc2bcade0>, 'mmi1x2_with_sbend': <function mmi1x2_with_sbend at 0x7f6cc2bcafc0>, 'mmi2x2': <function mmi2x2 at 0x7f6cc2bcb2e0>, 'mmi2x2_with_sbend': <function mmi2x2_with_sbend at 0x7f6cc2bcb420>, 'mmi_90degree_hybrid': <function mmi_90degree_hybrid at 0x7f6cc2bcb600>, 'mmi_tapered': <function mmi_tapered at 0x7f6cc2bcb920>, 'pad': <function pad at 0x7f6cc2bcbba0>, 'pad_array': <function pad_array at 0x7f6cc2bcbe20>, 'pad_gsg_short': <function pad_gsg_short at 0x7f6cc2bf80e0>, 'pads_shorted': <function pads_shorted at 0x7f6cc2bf8360>, 'rectangle_with_slits': <function rectangle_with_slits at 0x7f6cc2bf84a0>, 'cavity': <function cavity at 0x7f6cc2bf8720>, 'cdsem_all': <function cdsem_all at 0x7f6cc2bf89a0>, 'cdsem_bend180': <function cdsem_bend180 at 0x7f6cc2bf8b80>, 'cdsem_coupler': <function cdsem_coupler at 0x7f6cc2bf8cc0>, 'cdsem_straight': <function cdsem_straight at 0x7f6cc2bf8fe0>, 'cdsem_straight_density': <function cdsem_straight_density at 0x7f6cc2bf9120>, 'bendu_double': <function bendu_double at 0x7f6cc2bf93a0>, 'straight_double': <function straight_double at 0x7f6cc2bf94e0>, 'cutback_2x2': <function cutback_2x2 at 0x7f6cc2bf9620>, 'cutback_bend': <function cutback_bend at 0x7f6cc2bf99e0>, 'cutback_bend90': <function cutback_bend90 at 0x7f6cc2bf9b20>, 'staircase': <function staircase at 0x7f6cc2bf9c60>, 'cutback_bend180': <function cutback_bend180 at 0x7f6cc2bf9da0>, 'cutback_component': <function cutback_component at 0x7f6cc2bf9ee0>, 'cutback_splitter': <function cutback_splitter at 0x7f6cc2bfa2a0>, 'greek_cross': <function greek_cross at 0x7f6cc2bfa3e0>, 'greek_cross_with_pads': <function greek_cross_with_pads at 0x7f6cc2bfa660>, 'litho_calipers': <function litho_calipers at 0x7f6cc2bfa840>, 'litho_ruler': <function litho_ruler at 0x7f6cc2bfab60>, 'litho_steps': <function litho_steps at 0x7f6cc2bfad40>, 'resistance_meander': <function resistance_meander at 0x7f6cc2bfae80>, 'resistance_sheet': <function resistance_sheet at 0x7f6cc2bfb060>, 'verniers': <function verniers at 0x7f6cc2bfb240>, 'text': <function text at 0x7f6cc2bfbf60>, 'text_lines': <function text_lines at 0x7f6cc273c180>, 'text_klayout': <function text_klayout at 0x7f6cc273c2c0>, 'text_freetype': <function text_freetype at 0x7f6cc273c5e0>, 'pixel_array': <function pixel_array at 0x7f6cc273c860>, 'text_rectangular': <function text_rectangular at 0x7f6cc273c9a0>, 'text_rectangular_multi_layer': <function text_rectangular_multi_layer at 0x7f6cc273cae0>, 'pixel': <function pixel at 0x7f6cc2bfb560>, 'qrcode': <function qrcode at 0x7f6cc273c7c0>, 'version_stamp': <function version_stamp at 0x7f6cc273cb80>, 'disk': <function disk at 0x7f6cc273d120>, 'disk_heater': <function disk_heater at 0x7f6cc273d260>, 'ring': <function ring at 0x7f6cc273d4e0>, 'ring_crow': <function ring_crow at 0x7f6cc273d6c0>, 'ring_crow_couplers': <function ring_crow_couplers at 0x7f6cc273d800>, 'ring_double': <function ring_double at 0x7f6cc273d9e0>, 'ring_double_bend_coupler': <function ring_double_bend_coupler at 0x7f6cc273dd00>, 'ring_double_heater': <function ring_double_heater at 0x7f6cc273de40>, 'ring_double_pn': <function ring_double_pn at 0x7f6cc273e160>, 'ring_single_pn': <function ring_single_pn at 0x7f6cc273e2a0>, 'ring_single': <function ring_single at 0x7f6cc273e480>, 'ring_single_array': <function ring_single_array at 0x7f6cc273e5c0>, 'coupler_bend': <function coupler_bend at 0x7f6cc273e700>, 'coupler_ring_bend': <function coupler_ring_bend at 0x7f6cc273e840>, 'ring_single_bend_coupler': <function ring_single_bend_coupler at 0x7f6cc273e980>, 'ring_single_dut': <function ring_single_dut at 0x7f6cc273eb60>, 'delay_snake': <function delay_snake at 0x7f6cc273ede0>, 'delay_snake2': <function delay_snake2 at 0x7f6cc273efc0>, 'delay_snake_sbend': <function delay_snake_sbend at 0x7f6cc273f240>, 'spiral': <function spiral at 0x7f6cc273f4c0>, 'spiral_double': <function spiral_double at 0x7f6cc273f6a0>, 'spiral_racetrack': <function spiral_racetrack at 0x7f6cc273f920>, 'spiral_racetrack_fixed_length': <function spiral_racetrack_fixed_length at 0x7f6cc273fa60>, 'spiral_racetrack_heater_metal': <function spiral_racetrack_heater_metal at 0x7f6cc273fc40>, 'spiral_racetrack_heater_doped': <function spiral_racetrack_heater_doped at 0x7f6cc273fd80>, 'spiral_inductor': <function spiral_inductor at 0x7f6cc273fec0>, 'hline': <function hline at 0x7f6cc2770180>, 'optimal_90deg': <function optimal_90deg at 0x7f6cc27702c0>, 'optimal_hairpin': <function optimal_hairpin at 0x7f6cc27705e0>, 'optimal_step': <function optimal_step at 0x7f6cc2770720>, 'snspd': <function snspd at 0x7f6cc2770900>, 'interdigital_capacitor': <function interdigital_capacitor at 0x7f6cc2770ae0>, 'ge_detector_straight_si_contacts': <function ge_detector_straight_si_contacts at 0x7f6cc2770d60>, 'edge_coupler_silicon': <function edge_coupler_silicon at 0x7f6cc2770fe0>, 'edge_coupler_array': <function edge_coupler_array at 0x7f6cc2771120>, 'edge_coupler_array_with_loopback': <function edge_coupler_array_with_loopback at 0x7f6cc2771260>}, virtual_factories={'virtual_bend_circular': <function virtual_bend_circular_factory.<locals>.virtual_bend_circular at 0x7f6ccce8a340>, 'bend_euler': <function virtual_bend_euler_factory.<locals>.bend_euler at 0x7f6ccce8a5c0>, 'virtual_straight': <function virtual_straight_factory.<locals>.virtual_straight at 0x7f6ccce8a840>, 'bend_circular_all_angle': <function bend_circular_all_angle at 0x7f6cc2ceab60>, 'bend_euler_all_angle': <function bend_euler_all_angle at 0x7f6cc2ceb6a0>, 'straight_all_angle': <function straight_all_angle at 0x7f6cc2b6fec0>}, kcells={0: Unnamed_0: ports [], 0 instances, 1: bend_euler_R10_A90_P0p5_2f1f5c6d: ports ['o1', 'o2'], 0 instances, 2: ring_single_G0p2_R5_LX4_4982a41b: ports ['o2', 'o1'], 6 instances, 3: coupler_ring_G0p2_R5_LX_ba10ec54: ports ['o1', 'o2', 'o3', 'o4'], 0 instances, 4: coupler90_G0p2_R5_Bbend_7f6b3237: ports ['o1', 'o4', 'o2', 'o3'], 2 instances, 5: bend_euler_R5_A90_P0p5__c7bd5c45: ports ['o1', 'o2'], 0 instances, 6: straight_L5_N2_CSstrip_WNone: ports ['o1', 'o2'], 0 instances, 7: coupler_straight_L4_G0p2_CSstrip: ports ['o1', 'o2', 'o4', 'o3'], 2 instances, 8: straight_L4_N2_CSstrip_WNone: ports ['o1', 'o2'], 0 instances, 9: straight_L3_N2_CSstrip_WNone: ports ['o1', 'o2'], 0 instances, 10: straight_L0p6_N2_CSstrip_WNone: ports ['o1', 'o2'], 0 instances, 11: grating_coupler_ellipti_7b8aa449: ports ['o1', 'o2'], 0 instances, 12: taper_L40p0800000000000_2f7a994d: ports ['o1', 'o2'], 0 instances, 13: Unnamed_13: ports ['o2', 'o1', 'loopback1', 'loopback2'], 20 instances, 14: bend_euler_RNone_A90_P0_d7c3a5a9: ports ['o1', 'o2'], 0 instances, 15: straight_L43p5_N2_CSstrip_WNone: ports ['o1', 'o2'], 0 instances, 16: straight_L28p471_N2_CSs_e1a99baa: ports ['o1', 'o2'], 0 instances, 17: straight_L45p08_N2_CSstrip_WNone: ports ['o1', 'o2'], 0 instances, 18: straight_L321_N2_CSstrip_WNone: ports ['o1', 'o2'], 0 instances, 19: ring_single_G0p2_R20_LX_ecab943c: ports ['o2', 'o1'], 6 instances, 20: coupler_ring_G0p2_R20_L_9eeb3953: ports ['o1', 'o2', 'o3', 'o4'], 0 instances, 21: coupler90_G0p2_R20_Bben_b28db716: ports ['o1', 'o4', 'o2', 'o3'], 2 instances, 22: bend_euler_R20_A90_P0p5_b207852f: ports ['o1', 'o2'], 0 instances, 23: straight_L20_N2_CSstrip_WNone: ports ['o1', 'o2'], 0 instances, 24: Unnamed_24: ports ['o2', 'o1', 'loopback1', 'loopback2'], 20 instances, 25: straight_L28p5_N2_CSstrip_WNone: ports ['o1', 'o2'], 0 instances, 26: ring_single_G0p3_R5_LX4_349be076: ports ['o2', 'o1'], 6 instances, 27: coupler_ring_G0p3_R5_LX_8c040edf: ports ['o1', 'o2', 'o3', 'o4'], 0 instances, 28: coupler90_G0p3_R5_Bbend_1ec687c8: ports ['o1', 'o4', 'o2', 'o3'], 2 instances, 29: coupler_straight_L4_G0p3_CSstrip: ports ['o1', 'o2', 'o4', 'o3'], 2 instances, 30: Unnamed_30: ports ['o2', 'o1', 'loopback1', 'loopback2'], 20 instances, 31: ring_single_G0p3_R20_LX_ba30d425: ports ['o2', 'o1'], 6 instances, 32: coupler_ring_G0p3_R20_L_f3c74750: ports ['o1', 'o2', 'o3', 'o4'], 0 instances, 33: coupler90_G0p3_R20_Bben_4f150754: ports ['o1', 'o4', 'o2', 'o3'], 2 instances, 34: Unnamed_34: ports ['o2', 'o1', 'loopback1', 'loopback2'], 20 instances}, layers=<aenum 'LAYER'>, infos=LayerInfos(), layer_stack=LayerStack(layers={}), netlist_layer_mapping={}, sparameters_path=None, interconnect_cml_path=None, constants=Constants(), rename_function=<function rename_clockwise_multi at 0x7f6cd83936a0>, info=Info(), settings=KCellSettings(version='0.23.1', klayout_version='0.29.11', meta_format='v3'), future_cell_name=None, decorators=<kfactory.decorators.Decorators object at 0x7f6cd83fef50>) function_name=None basename=None boundary=None insts=<gdsfactory.component.ComponentReferences object at 0x7f6c973a63d0> size_info=<kfactory.kcell.SizeInfo object at 0x7f6c973d1790> dsize_info=<kfactory.kcell.DSizeInfo object at 0x7f6c973d2050> routes={}
Ipython#
This Jupyter Notebook uses an Interactive Python Terminal (Ipython). So you can interact with the code.
For more details on Jupyter Notebooks, you can visit the Jupyter website.
The most common trick that you will see is that we use ?
to see the documentation of a function or help(function)
gf.components.coupler?
help(gf.components.coupler)
Help on function coupler in module gdsfactory.components.couplers.coupler:
coupler(gap: 'float' = 0.236, length: 'float' = 20.0, dy: 'Delta' = 4.0, dx: 'Delta' = 10.0, cross_section: 'CrossSectionSpec' = 'strip', allow_min_radius_violation: 'bool' = False, bend: 'ComponentSpec' = 'bend_s') -> 'Component'
Symmetric coupler.
Args:
gap: between straights in um.
length: of coupling region in um.
dy: port to port vertical spacing in um.
dx: length of bend in x direction in um.
cross_section: spec (CrossSection, string or dict).
allow_min_radius_violation: if True does not check for min bend radius.
bend: input and output sbend components.
.. code::
dx dx
|------| |------|
o2 ________ ______o3
\ / |
\ length / |
======================= gap | dy
/ \ |
________/ \_______ |
o1 o4
coupler_straight coupler_symmetric
To see the source code of a function you can use ??
gf.components.coupler??
To time the execution time of a cell, you can add a %time
on top of the cell
%time
def hi() -> None:
print("hi")
hi()
CPU times: user 3 μs, sys: 1e+03 ns, total: 4 μs
Wall time: 5.72 μs
hi
For more Ipython tricks you can find many resources available online
gdsfactory downloads#
You can also use python plot the downloads for gdsfactory over the last year.
import datetime
import plotly.graph_objects as go
import requests
downloads0 = 0
def get_total_downloads(package_name):
statistics = []
end_date = datetime.date.today()
while True:
url = f"https://pypistats.org/api/packages/{package_name}/overall"
response = requests.get(url, params={"last_day": end_date})
data = response.json()
if response.status_code != 200:
return None
statistics.extend(
[(entry["date"], entry["downloads"]) for entry in data["data"]]
)
if "next_day" in data:
end_date = data["next_day"]
else:
break
statistics.sort(key=lambda x: x[0]) # Sort by date
dates, downloads = zip(*statistics)
cumulative_downloads = [
sum(downloads[: i + 1]) + downloads0 for i in range(len(downloads))
]
return dates, cumulative_downloads
# Replace 'gdsfactory' with the package you want to check
package_name = "gdsfactory"
dates, cumulative_downloads = get_total_downloads(package_name)
if dates and cumulative_downloads:
fig = go.Figure(data=go.Scatter(x=dates, y=cumulative_downloads))
fig.update_layout(
xaxis=dict(title="Date", tickformat="%Y-%m-%d", tickangle=45, showgrid=True),
yaxis=dict(title="Total Downloads", showgrid=True),
title=f"Total Downloads - {package_name}",
)
fig.update_layout(autosize=False, width=800, height=600)
fig.show()
else:
print(f"Failed to retrieve download statistics for package '{package_name}'.")